(Please note in this question I'll be comparing Python and C#)
I thought of an interesting question today, in C# {} braces are used to define a code block , in Python white space indentation is used to define a block. I'm curious to find out if this has any affect on compiler speed.
Python:
x = 1
if x == 1:
# indented four spaces
print("Hello World")
C#
x = 1
if(x == 1)
{
Console.WriteLine("Hello World");
}
I understand that both compilers are created to deal with the specified syntax, but would using different code block identities be a disadvantage to a compiler?
The answer is basically either "no" or "the question doesn't even make any sense".
Modern compilers generally have two front end parts, a scanner (usually a simple regular-expression based finite state machine with no memory other than its state, although some languages require a bit of hairy feedback between their parsers and scanners) and a parser (which uses some variant of a stack-based machine called a pushdown automaton).
The scanner does simple lexical analysis and turns the input stream into a series of tokens. The parser then recognizes these tokens according to a grammar (typically a context-free grammar). The output from the parser is an abstract syntax tree or AST. The AST represents the program; by this point, silly little details like brace blocks vs indentation are gone.
The bulk of compilation time for most languages occurs after this point. However, for some languages, the lexical analysis time is significant. Since Python does relatively little compile-time optimization, the speed of the scanner can be fairly significant. Still, the way blocks are defined (by indentation instead of braces) has little impact on scanner performance: it's much more dominated by clever algorithms for allocating tokens, and the speed of the finite state machine that implements the scanning.