Do comments make the code run slower?

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!';

?>

Tags:

Php

Comments