1

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 :)

BlackCoder
  • 115
  • 1
  • 2
  • 13
  • If you're using a PHP accelerator (which, if you care about performance at all you should be doing) then no, they do not cache the source code but the byte code which is the same no matter the white spaces. – Joachim Isaksson Aug 29 '13 at 17:49
  • You could do a test for yourself. Save the current time, Create a loop of 100+ iteration an call your function.,save the finish time and display how long the process took. Now remove all the additional stuff from your function (whitespace etc) and re-run the loop and see the difference. –  Aug 29 '13 at 17:51
  • @JoachimIsaksson thanks :) but i never hear about PHP accelerator. i will search for it now.. thanks again :) – BlackCoder Aug 29 '13 at 17:57
  • @jeff thanks for your time :) i will do this :-) and i will update my results here :) – BlackCoder Aug 29 '13 at 17:58
  • @BlackCoder [This](http://en.wikipedia.org/wiki/List_of_PHP_accelerators) will get you started. – Joachim Isaksson Aug 29 '13 at 18:02
  • @JoachimIsaksson thanks :) Downloaded one from here ( http://pecl.php.net/package/APC ) after seeing this... my face turned into a Question Mark :O – BlackCoder Aug 29 '13 at 18:04
  • 1
    @BlackCoder [Here](http://se1.php.net/manual/en/apc.installation.php) are the installation instructions for APC (read the comments, your OS may be covered there) – Joachim Isaksson Aug 29 '13 at 18:12
  • @JoachimIsaksson thanks again :) seems it will help me – BlackCoder Aug 29 '13 at 18:15

4 Answers4

2

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!

2

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.

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • MonkeyZeus, thanks :) i learn a new thing :-) yes i write more and more comments the file size increase and my server have to ready that big file :) if i use less comments... my server may read the same file 2 times instead if 1 time in a second. – BlackCoder Aug 29 '13 at 18:15
  • It's completely negligible. I have plenty of files with 1000+ lines of code and they are around 8KB. You should always consider the size of your `include()` files as well though. If you want to understand how the performance of your hard drive affects stuff then you need to research something called "Seek Time" and "Allocation Unit Size". After you are done researching then you might just buy an SSD :) – MonkeyZeus Aug 29 '13 at 18:19
1

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.

Logan Murphy
  • 6,120
  • 3
  • 24
  • 42
0

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.

James
  • 4,644
  • 5
  • 37
  • 48