Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does comments makes the run program slow?

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!

like image 742
Saugat Avatar asked Nov 30 '13 08:11

Saugat


People also ask

Do comments slow down a program?

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.

Do code comments affect performance?

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.

Can too many comments slow your code in?

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).

Do comments affect compile time?

Yes, each comment that you write will make the compilation slower, because the compiler has to read more text.


Video Answer


1 Answers

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.

like image 64
Prashant Ghimire Avatar answered Sep 29 '22 15:09

Prashant Ghimire