[ACCEPTED]-Do comments make the code run slower?-comments

Accepted answer
Score: 20

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

Score: 15

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

<?php /* A comment */ ?>

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

Score: 5

well just for fun I tried this: (code below) with 7 10.000 lines of Lorem ipsum dummy text, one 6 commented out, one no text.

results were: on 5 (php 7 freebsd 12 mint server). minuscule 4 difference !

It took 7.8678131103516E-6 seconds! (with 3 10.000 lines of commented out text It took 2 5.0067901611328E-6 seconds! (no text at 1 all)


$start = microtime(true);
1000 lines of Lorem ipsum 
$end = microtime(true);
echo '<BR>It took ' . ($end-$start) . ' seconds!';

Score: 4

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

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

Score: 0

@Degar007 posted important info that alone 9 answers the OP: 10,000 lines of comments 8 take 2 seconds to interpret. Between Wordpress, its 7 plugins, and its theme, there may be at 6 least 10,000 lines of comments. So, by removing 5 all comments from WP we could speed each 4 site's initial load time by 2 seconds. Multiply 3 that by tens of millions of PHP CMSs, CRMs 2 etc, around the globe, and that's 1 megawatt 1 of power saved, right there.

More Related questions