Do variable names affect website performance? I know this will going to be very low number, but still can any one provide the reasons for not choosing a long variable name in aspect of performance?
-
2Please clarify your question - where is this variable and what language is it written in?– Luke GrahamCommented Jul 13, 2011 at 14:13
-
17You should never, EVER choose short and illogical variable names if your reasoning is (questionable) performance. Source code is there for other people to read it, not for satisfying computers and their microscopic performance gains.– Michael J.V.Commented Jul 13, 2011 at 14:22
-
i am using PHP..– AvinashCommented Jul 13, 2011 at 14:28
-
2...And you own xpertdeveloper.com?– Jim G.Commented Jul 13, 2011 at 19:25
-
3Hi Avinash can you explain in your question your rationale for thinking that this might be the case?– user8Commented Jul 13, 2011 at 21:49
8 Answers
No, it will not. Generally speaking, when code is compiled, the variable names are replaced by the memory address they refer to. Computers don't know anything about variable names; they only want to know where values are stored.
Variables are symbols, nothing more. They replace hex values with names so that programmers have an easier time understanding what they're doing. So, there will be no performance boost by choosing shorter variable names.
That said, you may get miniscule (and I'm talking microscopic) improvements in compile times and the JIT first interpretation, but that is only because the parser takes a few CPU cycles less to read the variable name. This is a one-time cost, and is statistically insignificant when worrying about performance.
-
13While your answer is correct for application programming, the O.P is referring to Websites, and therefore there is a chance he is using an interpreted language. In such a case, the code would need to be read in from file, then interpreted. In this case, a longer variable name will take longer to load & parse. However the performance gain will be insignificant, and massively offset by the extra hassle to the programmer of reading and understanding the code. Commented Jul 13, 2011 at 14:25
-
1@Gavin It is correct for interpreted languages as well - "JIT first interpretation". Most interpreted languages now compile as they execute rather than line by line execution, AFAIK. Commented Jul 13, 2011 at 14:39
-
1Michael - in order to compile, the file must first be read into memory. This process will take longer depending on the length of the file. Longer variable names = longer files. Commented Jul 13, 2011 at 14:42
-
The question is tagged as PHP. PHP doesn't yet have JIT - that will be a new feature of the 8.0 release planned for late this year. It does compile to byte code before execution, and the bytecode is generally cached on webservers to be used in multiple requests.– bdslCommented Mar 15, 2020 at 12:04
can any one provide the reasons for not choosing a long variable name in aspect of performance?
Michael covered the answer (i.e. No) but variable names do affect programmer performance. If you bring on a new developer or someone who's unfamiliar with the code, then having long and/or confusing variable names can be distracting and slow the comprehension process.
In general, you want to use short, descriptive variable names because they're easier to read. Imagine if you have to ignore your code for 10 years and then understand everything again. Would you rather read "getInput" or "getInputFromUserWhoInputsStringOrElseInformReaderOfError" ? (exaggeration of course :P)
There are times, however, when having a slightly longer name can be beneficial. For example, getBirthdayInput() would be much more descriptive than getInput(). You want to simplify to a point but oversimplification can also be problematic.
-
7for reading long names is better, if you find "getInputFromUserWhoInputsStringOrElseInformReaderOfError" you don't need to read documentation what's this function, if you find "getInput" you need. And after 10 years documentation will be wrong, or incomplete, or missing. getInputFromUserWhoInputsStringOrElseInformReaderOfError is certainly to long, but to understand about what it is long is better (and I don't care that women say that size does not matter).– DainiusCommented Jul 13, 2011 at 14:31
-
I disagree in this case. I think just by reading code it would be pretty easy to understand what getInput() does, especially if it prints "invalid input" or something right afterward. There are definitely times when a longer name can be better though - I'll edit it in! Commented Jul 13, 2011 at 14:34
-
ofc it depends from context, but in my experience longer name is better (but not so long as getInputFromUserWhoInputsStringOrElseInformReaderOfError). And context really important as file.read() is quicker than file.readAllFileAsByteArray(). But as I said usually IMHO longer name provides more information.– DainiusCommented Jul 13, 2011 at 14:45
-
1If the documentation is wrong, incomplete, or missing, the codebase is doomed anyway. Commented Jul 13, 2011 at 16:01
-
2This is answering a question that wasn't asked.– user8Commented Jul 14, 2011 at 20:29
Unless you're using op-code cache (also known as "PHP accelerators"), then there is indeed an impact. But that impact is so low, that it can be neglected. If you do use op-code cache, then there is zero impact.
-
1and if you care about performance so much that you consider shortening variable names to gain a few cycles, not using op-code cache would be criminal... and switching to a compiled language like C would be recommended.– SF.Commented Jul 13, 2011 at 23:45
-
But obviously that impact is accumulative, so the larger the application, the larger the impact, right?– n00dlesCommented Jul 18, 2017 at 13:38
-
The Zend Opcache has been shipped with PHP for several years now, so everyone should be using it. I think it is generally enabled by default.– bdslCommented Mar 15, 2020 at 12:07
While Michael is correct for application programming, your question is referring to web development with PHP, which is an interpreted language. In such a case, the code would need to be read in from file, then interpreted. In this case, a longer variable name will take longer to load & parse.
The performance hit with doing so however will be insignificant, and will probably be in the region of fractions of a millisecond for an entire script. You could always try it out with a sample script, and use a timing method such as that detailed at http://www.developerfusion.com/code/2058/determine-execution-time-in-php/ but this will probably not start timing until after the file has been read in. Additionally, the execution time between retries will vary far more than the difference between variable name lengths, so you will need to perform a significant number of retries and take the average of each before you can obtain a remotely meaningful average.
As BlackJack points out, longer names can be much more difficult to understand, and take a lot of extra effort to type out (and are much more prone to typo's). While there may be a tiny performance gain, this does not justify the extra hassle created for the programmer. As such, short, concise and easy to understand variable names are preferred.
So in short, don't worry about the variable length name, but instead concentrate on writing clean, meaningful code.
Maybe:
Server code is typically compiled and variable name length will not affect it for the reasons mentioned. However, if the variable name is used to build markup various string operations. Since the HTTP response (containing markup/returned json/returned data) is larger, it will take slightly longer, although the difference will be negligible. If JavaScript is not minified, it will be a larger file, taking longer to travel to the client.
Other than minifying JavaScript files, any optimisation efforts for a web application/web site, will be better spent on other aspects.
Yes, it will, but not in the sense you are thinking about.
With bad variable names, developers will get easily confused in the source code. It will be hard to read and hard to understand.
In the end, the source code will be hard to maintain and it will be nearly impossible to make it evolve. This will lead inevitably to higher maintenance costs, higher development costs, more bugs, and poorer performances.
Variable name will have absolutely no influence at runtime, and totally negligible at compile time. But bad names will inevitably lead to bad performance because nobody understand the code and it ends up in a stack of hack one on top of another, making things worse each time.
Read these to know about good variable name: http://tottinge.blogsome.com/meaningfulnames
Note that if you feel the need of very long variable name, it means that your code is poorly architectured. A variable name is always expressed in a context: namespace, class name, filename, folder, function name, etc. Thus, if the name need to be long to be explicit, this means that the thing that you are trying to name DOESN'T BELONG HERE. In this case, think about putting this code in the appropriate place, or create that place if it doesn't exist yet.
The answer is obviously yes, in regards to php.
The answers that say it will not make a difference are not necessarily correct. For example, I have a small php script, just a few lines, but it must loop about 1,950,000 times before it finishes its job, so although a single run of the script may not be noticed, those fractions of a second add up substantially when looping many times.
-
1But you are wrong. Any decent php interpreter will parse the code only once. The guys writing php interpreters are not stupid. Commented Mar 15, 2020 at 10:45
-
@gnasher729 You are making assumptions about how the interpreter or the entire server works and you should never do that. Even if it will only parse once per run, it may re-parse on every run and not cache a parsed representation (other systems will do that but you cannot know in advance). And generally the more bytes a script is the longer it will take to parse. So Diorthotis is not wrong in generally, s/he may only be wrong regarding a specific system.– MeckiCommented Mar 24, 2020 at 1:02
You can use short names that make your code less readable. You'll save a microsecond or two. But because the code is less readable, it is harder to improve the code and make it faster. So your unmaintainable, unimprovable code with short names will end up running significantly slower in the end.
So to answer your question ("does it affect performance"): Yes, but not in the way you like.