View Single Post
  #36 (permalink)  
Old 11-06-2007, 06:20 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: PHP Optimization Tips

Another useful optimization technique for PHP would be changing how you use 'for' loops. Most people use the obligatory (assuming $j is an array):

for ($i = 0; $i < count($j); $i++) {
...
}

There are two obvious performance hits here. The first, as mentioned in your blog, is the post-increment. You can EITHER do ++$i, or you can change it to '$i = $i + 1', respectively. The second thing would be to move the count() function out of the middle of the 'for' loop, to avoid the multiple calls on count(). So, the above example could easily written out in a new fashion:

for ($i = 0, $k = count($j); $i < $k; $i = $i + 1) {
...
}

I have found that in my code writing I am able to shave a few microseconds off it's execution; on bigger, more robust applications, those microseconds have added up to whole seconds!
__________________
Thanks & Regards
Sabari...
Reply With Quote