Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Do long (very long) variable names slow down the compilation of source code?

I'm aware that the length of variables has 0% impact on interpretation as the compiler changes them to machine code which is always the same length. But my question is, does the speed at which the compiler vary depending on the length of variables?

For example, if I have a variable name which is a million characters long, surely the compiler would struggle as for it to recognise the variable is a variable, it would need to read the entire thing first which would surely take longer depending on its size?

To clarify, what process do typical (the majority) of high level language compilers use when changing a source-code's variable name to a machine code name?

Do they find the start of the variable working from the left and then find the end working from the right, do they read through the entire variable name (one character at a time) until the end is found or do they not even look at the variable name at all and just know that the code requires a variable of type int (for example) and assign it a machine-level name instantly and then completely ignore any user-defined variable names?

Note that I am a complete novice (still a student) so excuse my ignorance if I get my terms mixed up.

share|improve this question
2  
Sure, the parser might work a little harder, but in any imaginable usage case the difference should be entirely unmeasurable. –  Rotem Oct 10 '13 at 6:46
    
Like I said, my question is entirely hypothetical and I understand it has nothing to do with any actual usage case. I'm asking for the benefit of knowledge regarding compilers and nothing more. And by entirely unmeasurable, do you mean it makes 0% difference on compilation time or maybe 0.000001% difference? Even if it's a matter of nano-seconds I'm interested to know if it affects compilation time at all. –  Jazcash Oct 10 '13 at 6:52
3  
Most compilers have limits on identifier names, line lengths and file sizes. Therefore, it won't struggle much: it will just raise an error and terminate. –  mouviciel Oct 10 '13 at 7:11
    
As an aside: Machine code does not contain variable names. Instead, it operates on memory addresses which are equivalent, but fixed-width. It seems you have an interest in how parsers work. You might want to read up on them and maybe write one of your own. Certain libraries like Marpa (for Perl) can make this easier. –  amon Oct 10 '13 at 11:33
add comment

3 Answers

up vote 2 down vote accepted

The compiler (typically) works in several phases, the first being the "parsing" stage. During the parsing stage, the program is converted to an internal representation, usually referred to as an "Abstract Syntax Tree" (AST), that the remainder of the compiler stages work on.

The parsing step is, essentially, proportional to the amount of source code needed to be read. In this stage, the length of a variable name has (some) impact.

However, the time-consuming parts of the compiler tend to exclusively operate on the AST, where the speed of processing is independent of the length of identifiers.

So while it does have some impact, I'd expect typical (large) programs to have a compilationg time that is essentially-independent of identifier lengths.

share|improve this answer
add comment

The compiler have to build a symbol table to identify variables in your code. Here both foos refer to the same abstract concept:

foo = 1
bar = foo + 5

So to identify both foo as a single identity, you have to process the full word. So a long variable will take longer to process. But it should not be a concern. Readability should be your main concern when naming a variable.

share|improve this answer
    
This is nearer to what I wanted to know. More specifically, how does the compiler process the full word? If it takes longer to process, you're assuming the compiler reads through the variable name step by step until it finds the end, in which case compilation time increases (albeit very very minimally) with the length of the variable name. Why can't the compiler find the start of the variable name and then the end (working from right to left, identifying the variable name by finding the white-space) and then substituting the machine level identifier for the variable name? –  Jazcash Oct 10 '13 at 6:56
    
This way, it wouldn't matter how long the variable name is because the compiler never has to read through it, all it has to do is know that it exists? –  Jazcash Oct 10 '13 at 6:57
    
@Jazcash How does it find the whitespace without reading through the variable? –  Rotem Oct 10 '13 at 7:01
    
Can't it work from left to right to find the variable start and after it knows the start, can't it work from right to left to find the end? For example: string name = "jim" It would find the start of the variable name identified by the white-space after string, and then it finds the end by the white-space before the =. –  Jazcash Oct 10 '13 at 7:04
    
"and then it finds the end by the white-space before the =" - How does an algorithm find the white-space without going through every character? I'm sure compilers are naturally optimized to quickly identify 3-30 characters long symbols, not 1000000 characters long symbols. –  Rotem Oct 10 '13 at 7:12
show 1 more comment

Yes, it will change the compilation speed by a few nano to a few milliseconds.

It will obviously take longer to process a 10MB source file than 10 line hello world type source file.

Will the length of variable names change the compilation speed in any way that's perceptible to a human being? No.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.