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)
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.
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.
well just for fun I tried this: (code below) with 10.000 lines of Lorem ipsum dummy text, one commented out, one no text.
results were: on (php 7 freebsd 12 mint server). minuscule difference !
It took 7.8678131103516E-6 seconds! (with 10.000 lines of commented out text It took 5.0067901611328E-6 seconds! (no text at all)
<?php
$start = microtime(true);
/*
1000 lines of Lorem ipsum
*/
$end = microtime(true);
echo '<BR>It took ' . ($end-$start) . ' seconds!';
?>
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.
@Degar007 posted important info that alone answers the OP: 10,000 lines of comments take 2 seconds to interpret. Between Wordpress, its plugins, and its theme, there may be at least 10,000 lines of comments. So, by removing all comments from WP we could speed each site's initial load time by 2 seconds. Multiply that by tens of millions of PHP CMSs, CRMs etc, around the globe, and that's 1 megawatt of power saved, right there.
精彩评论