Question

i have this doubt from many days. in every PHP code i write, i will write many comments, leave many white spaces and leave 4 to 5 empty lines between section and section (to make it clear for me)

will all these empty spaces, comments, empty lines make my PHP code to run slow ?

personal experiences are much appreciated :)

Was it helpful?

Solution

This is really a matter of IO and hard drive speed. If your bare file is 10KB and comments and line breaks add 4KB then the extra time that the hard drive spends reading more KB is what you need to benchmark (it's negligible by the way), not even worth your time.

If you start getting into micro-optimization then you run the risk of making your code absolutely horrid to read and maintain.

The best way to speed up your code is to re-factor code where necessary and don't do silly things that obviously hog resources like this crude example:

<?php

$arr = array(); // pretend it has 50,000 items

//GOOD IDEA: count the array once and reference that number
$arr_count = count($arr);
for($i=0; $i < $arr_count; $i++){
    echo $arr[$i];
}


//BAD IDEA: re-counting the array for every iteration
for($i=0; $i < count($arr); $i++){
    echo $arr[$i];
}

?>

Also unsetting a large array after you are done using it is better than waiting for the Garbage Collector to kick in. For example: pulling data from DB and looping through it. Unset the data when done and keep coding.

OTHER TIPS

Comments and white space are completely ignored when the code is run. You can think of all that extra stuff as being completely wiped away once your done and the code is doing its thing.

Extra white space and comments are solely there for you and fellow coders to be better able to read and understand your code. In fact, if you don't use extra white space and comments, coders will get angry with you for writing and providing terrible code!

Consider the following code.

<?php
    $time = round(microtime(true) * 1000);
    for($i = 0; $i < 1000000; $i++) {
        /*


        */
    }
    echo (round(microtime(true) * 1000) - $time) . "<br/>";


    $time = round(microtime(true) * 1000);
    for($i = 0; $i < 1000000; $i++) {
    }
    echo (round(microtime(true) * 1000) - $time) . "<br/>";
?>

There are times that the first is faster and others that the second is fast. So comments do not affect the speed.

Not really, is the simple answer for general scripts and coding.
It's likely that if you were having to consider gaining a few milliseconds here and there, and removing comments was affective, A) you have too many comments, and B) you'd already know about it all and be performing benchmarks etc.

The amount of comments is usually proportionate to the amount of code you have. ie a line or two of comments for a load of IF/ELSE, setting vars to POST or SESSIONS etc, and DB queries etc. And as the majority of PHP's time parsing a script is opening the file, accessing memory, checking thousands of things including cache etc, reading and executing the code, accessing database etc, the time taken to ignore your comments is probably .001%

Comments are used by you, and possibly other developers, to understand the code. Just keep them neat and try to keep them as short as possible while remaining concise, factual and useful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top