April 10th, 2007 by William Yang
While searching for “php benchmark”, I found PHP Benchmark test page. The first benchmark the page showed was foreach(), while(list()=each()) and loop using array_keys(). The test result was stated as “strange”. Loop using array_keys() function out performed the other loop techniques by 2 to 9 folds. Of cause, the test was done in a PHP4 enviroment. Since PHP5 was introduced, there has been a lot of performance increases, the test is out dated.
Eagerly wanting to know how this will perform in PHP5, I have written a set of similar tests to be tested on my PHP5+Windows machine. The result is as expected: completely different, loop using array_keys() is no longer the best performed loop in PHP5.
The test array is made randomly generated 8 characters long string keys and 64 characters long string values.
Test results:
| Loop style |
Test 1 |
Test 2 |
Test 3 |
Test 4 |
Test 5 |
Average |
| ForEachLoop |
0.92 |
0.79 |
0.70 |
0.96 |
0.75 |
0.82 |
| WhileListEachLoop |
2.25 |
2.10 |
4.87 |
2.83 |
2.11 |
2.83 |
| ForEachLoopWithKey |
1.02 |
0.98 |
0.86 |
0.84 |
1.03 |
0.95 |
| WhileListEachLoopWithKey |
2.20 |
2.67 |
2.13 |
3.62 |
2.17 |
2.56 |
| GetKeyLoop |
3.76 |
6.00 |
3.30 |
3.24 |
4.26 |
4.11 |

Source code:
Loop 1: foreach() loop without fetching array key
Show Code
foreach ($test_array as $ele) {
$tmp[] = $ele;
}
Loop 2: while(list()=each()) loop without fetching array key
Show Code
while (list(,
$ele) =
each($test_array)) {
$tmp[] = $ele;
}
Loop 3: foreach() loop fetching array key
Show Code
foreach ($test_array as $k=>$ele) {
$tmp[] = $ele;
}
Loop 4: while(list()=each()) loop fetching array key
Show Code
while (list($k,
$ele) =
each($test_array)) {
$tmp[] = $ele;
}
Loop 5: loop using array_keys()
Show Code
for ($i=0; $i<$count; $i ) {
$tmp[] = $test_array[$keys[$i]];
}
Full Source:
Show Code |
Download looptest.txt
<?
function microtime_float()
{
return ((float)$usec + (float)$sec);
}
function createRandomString($len = 8) {
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
$i = 0;
$pass = '' ;
while ($i <= $len) {
$tmp =
substr($chars,
$num,
1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
$max_element = 1000;
// Initialize array, filling random keys and values
for ($i = 0; $i <= $max_element; $i++) {
$test_array[createRandomString(8)] = createRandomString(64);
}
function ForEachLoop() {
foreach ($test_array as $ele) {
$tmp[] = $ele;
}
}
function WhileListEachLoop() {
while (list(,
$ele) =
each($test_array)) {
$tmp[] = $ele;
}
}
function ForEachLoopWithKey() {
foreach ($test_array as $k=>$ele) {
$tmp[] = $ele;
}
}
function WhileListEachLoopWithKey() {
while (list($k,
$ele) =
each($test_array)) {
$tmp[] = $ele;
}
}
function GetKeyLoop() {
for ($i=0; $i<$count; $i++) {
$tmp[] = $test_array[$keys[$i]];
}
}
'ForEachLoop', 'WhileListEachLoop', 'ForEachLoopWithKey', 'WhileListEachLoopWithKey', 'GetKeyLoop'
);
foreach ($tests as $test) {
// Foreach test
for ($a=1; $a<=5; $a++) {
$timestart = microtime_float();
$time_took = microtime_float() - $timestart;
$test_score[$test][] = $time_took*1000;
}
}
?>
<table cellpadding=4 border=0 cellspacing=2>
<tr>
<th>Loop style</th>
<th>Test 1</th>
<th>Test 2</th>
<th>Test 3</th>
<th>Test 4</th>
<th>Test 5</th>
<th>Average</th>
</tr>
<? foreach ($tests as $test) { ?>
<tr>
<td><?=$test; ?></td>
<? foreach ($test_score[$test] as $score) { ?>
<? } ?>
</tr>
<? } ?>
</table>
This entry was posted
on Tuesday, April 10th, 2007 at 7:34 pm and is filed under PHP Code, Optimization.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.