Speed up your PHP for loops
Sep 20
About this post: Although PHP for loops are already incredibly fast, you can further, albeit rather marginally, optimize your loops if you are a performance junkie. Here are some simple ways how.
NOTE: The idea of writing this just spontaneously came to mind the other day, really. Actually, this might be a post I wrote just to demonstrate Phi, my own WordPress plugin
Here are a couple of simple ways to optimize your PHP for loops. For loops are already the fastest solutions to many situations over foreach and while loops, but you can further better the performance of your PHP scripts. The differences may seem marginal at first, but I figured why not?
Also, there are many other ways, I predict, to optimize your loops. These are the ones that just came into mind as I wrote this post. So enjoy!
You can run these blocks of code in the command line (without the delimiters) to see the timings for yourself.
1. Don’t call functions in the conditional check
This is pretty fundamental, you may already have come across this tip that indeed gives your script’s performance an even greater boost.
Please, don’t call functions in the conditional check (the second expression in the construct), whatever you do. It’ll get called over and over again, and you can imagine how dizzy PHP might end up after having counted the size of a thousand-large array a hundred times. Do the parser a favor, won’t you now?
Example 1: call functions outside the conditional check
<?php
/*
* An array of a million numbers, for illustration
* only. You don't always use arrays of numbers,
* much less come across such large ones as this...
*/
$a = range(1, 1000000);
for ($i = 1; $i <= 5; ++$i)
{
$s = microtime(true);
// Counting a million-large array a million times
// is nothing but torture
for ($j = 0; $j < count($a); ++$j)
{
$foo = 'bar';
}
$t = microtime(true) - $s;
printf("%d. %f secondsn", $i, $t);
}
for ($i = 1; $i <= 5; ++$i)
{
$s = microtime(true);
// No doubt it is a huge array, but PHP just counts
// it only once, then remembers the array count
for ($j = 0, $c = count($a); $j < $c; ++$j)
{
$foo = 'bar';
}
$t = microtime(true) - $s;
printf("%d. %f secondsn", $i, $t);
}
?>
1. 0.370603 seconds 2. 0.397640 seconds 3. 0.401025 seconds 4. 0.398929 seconds 5. 0.399258 seconds 1. 0.172618 seconds 2. 0.172244 seconds 3. 0.172764 seconds 4. 0.172037 seconds 5. 0.172171 seconds
2. Post-increment or pre-increment does matter
Did you notice in my above example that I used ++$i to increment $i?
Apparently, whether you tell PHP to evaluate $i++ or ++$i does make some kind of difference in performance. In this case, pre-incremental (++$i) is faster. No doubt you will never notice it since it’s faster only by a fraction of a millisecond, but your application, PHP, your server and ultimately your users will thank you.
In my opinion it makes the loop code look cooler. Perhaps the loop-progressing expression looking different has something to do with it.
Example 2: use pre-increment if you can
<?php
for ($i = 1; $i <= 5; ++$i)
{
$s = microtime(true);
// Post-incrementing
for ($j = 0; $j < 1000000; $j++)
{
$foo = 'bar';
}
$t = microtime(true) - $s;
printf("%d. %f secondsn", $i, $t);
}
for ($i = 1; $i <= 5; ++$i)
{
$s = microtime(true);
// Pre-incrementing
for ($j = 0; $j < 1000000; ++$j)
{
$foo = 'bar';
}
$t = microtime(true) - $s;
printf("%d. %f secondsn", $i, $t);
}
?>
1. 0.178003 seconds 2. 0.176616 seconds 3. 0.176738 seconds 4. 0.176573 seconds 5. 0.176910 seconds 1. 0.166239 seconds 2. 0.166349 seconds 3. 0.165796 seconds 4. 0.165859 seconds 5. 0.165786 seconds
Comments closed
Sorry, but commenting for this post has been closed.