Do comments make the code run slower?

和自甴很熟 提交于 2019-12-08 15:40:37

问题


I heard that a heavily commented script runs slightly slower than a non-commented one. Is it true?

Did anyone test this? (like how much slower is it in percentages)


回答1:


Commenting will not affect the script execution time in normal case. But the number of lines you write in your code affect the parser to read and buffer it considerably. If you can execute certain things in 20 lines, you try to write the same thing in 1000 lines, the performance might be affecting if its part of an application which executes sequentially. Even if few lines or lot of lines the dependencies are important. If you are using a library which is heavily depending on some applications, obviously the loading time, parsing time and compile and execution time etc will increase. In any case the commenting will not affect considerably, but a few microseconds will not cost you much. So go ahead and comment your code and make it readable by co-developers.




回答2:


I can tell you that 99.99% of the time spent parsing the following file:

<?php /* A comment */ ?>

Is spent on opening the file, reading its contents, and closing the file. If you copied and pasted that comment onto 10,000 lines, it'll make no difference.




回答3:


If your code is compiled then the comments will be stripped out during the parsing, so will not even be included in your finished bytecode, meaning there is no difference.

If your code is interpreted, then sure the compiler needs to strip the comment lines out, but far more time is spent executing your program, so the different is negligible.



来源:https://stackoverflow.com/questions/5423344/do-comments-make-the-code-run-slower

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!