Commenting in your source code is generally considered a good practice. But is there any chance that commenting in a program's source code making the program execution slow ? Thanks in advance!
So no, comments do not slow down the programs at run-time. Note: Interpreters that do not use some other inner structure to represent the code other than text, ie a syntax tree, must do exactly what you mentioned.
It can prevent functions from being inlined, which affects performance, though this shouldn't really happen. Show activity on this post. In some perhaps isolated circumstances, comments definitely somehow bog down the code execution.
Comments are stripped from the compiled code, they can not slow down your compiled program. All the comments can do is to slow down the compiler (it should parse the source code and cut the comments off).
Yes, each comment that you write will make the compilation slower, because the compiler has to read more text.
No, not in Java. Comments are removed when you compile your code.
Further explaining, it depends on the type of programming language you are using. For compiled programs, only the executable files are used by the computer during running the program instead of the source files. For example in java, .class
files files does not have any traces of comments to make the program slow.
In case of interpreted languages like PHP, interpreter has to know that its a comment on every line starting with //
. So it might take a faction of seconds (usually negligible) more time to execute.
But in case web languages like HTML and JavaScript, the comments are actually fetched to the client. When you click on view source of a webpage, you can see the actual HTML and JavaScript comments. This ofcourse will have to be loaded to the machine and will take considerable amount of time. Therefore, we care about minifying HTML, CSS and JS in the production environment.
So to sum up, it depends on programming languages whether the comments make a program slower.
Hope it was helpful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With