Saugat
Saugat

Reputation: 69

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!

Upvotes: 5

Views: 2451

Answers (2)

Prashant G
Prashant G

Reputation: 4910

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.

Upvotes: 17

munificent
munificent

Reputation: 12384

No, in Java the comments will be removed by the compiler before your code is ever run.

Upvotes: 4

Related Questions