Categories
PHP

PHP Foreach Loop Benchmark – Optimize your code & Keep the server happy!

Keep the internet as energy efficient as possible and

the easiest ways to reduce your carbon footprint is to optimize your code.

The future of humanity and the planet is in your hands!

The execution speed is very clear from the image below, each variant is sorted according to the speed of execution, the first is the fastest:

PHP Benchmark Foreach Loop

Below is a code by which you can evaluate the performance of php foreach loop on your server.

<?php

$test = [
['a1' => 1, 'a2' => 'test', 'a3' => 'demo', 'a4' => 'end',],
['a1' => 2, 'a2' => 'test', 'a3' => 'demo', 'a4' => 'end',],
['a1' => 3, 'a2' => 'test', 'a3' => 'demo', 'a4' => 'end',],
];

$x = '';
$endtime = [];
$formode = [];
for($j=0; $j<10; $j++) {

$time = microtime(true);
for($i=0; $i<100000; $i++) {
foreach ($test as ['a4' => $a4] ) { $x = $a4; } // 0.17695760726929
}
$endtime[1][$j] = microtime(true)-$time;
echo $endtime[1][$j].', ';
$formode[1] = 'foreach ($test as [\'a4\' => $a4] ) { $x = $a4; } // a.k.a. Destructured Array';

//~~~~~~~~~~~~~~~~~~~~~~
$time = microtime(true);
for($i=0; $i<100000; $i++) {
foreach ($test as $val ) { $x = $val['a4']; } // 0.17695760726929
}
$endtime[2][$j] = microtime(true)-$time;
echo $endtime[2][$j].', ';
$formode[2] = 'foreach ($test as $val ) { $x = $val[\'a4\']; } ';

//~~~~~~~~~~~~~~~~~~~~~~
$time = microtime(true);
for($i=0; $i<100000; $i++) {
foreach ($test as &$val ) { $x = $val['a4']; } // 0.17695760726929
}
$endtime[3][$j] = microtime(true)-$time;
echo $endtime[3][$j].', ';
$formode[3] = 'foreach ($test as &$val ) { $x = $val[\'a4\']; } // With References';

//~~~~~~~~~~~~~~~~~~~~~~
$time = microtime(true);
for($i=0; $i<100000; $i++) {
foreach ($test as $key=>$val ) { $x = $val['a4']; } // 0.17695760726929
}
$endtime[4][$j] = microtime(true)-$time;
echo $endtime[4][$j].', ';
$formode[4] = 'foreach ($test as $key=>$val ) { $x = $val[\'a4\']; } // Key + Value Syntax';

//~~~~~~~~~~~~~~~~~~~~~~
$time = microtime(true);
for($i=0; $i<100000; $i++) {
foreach ($test as $key=>&$val ) { $x = $val['a4']; } // 0.17695760726929
}
$endtime[5][$j] = microtime(true)-$time;
echo $endtime[5][$j].', ';
$formode[5] = 'foreach ($test as $key=>&$val ) { $x = $val[\'a4\']; }';

//~~~~~~~~~~~~~~~~~~~~~~
$time = microtime(true);
for($i=0; $i<100000; $i++) {
foreach ($test as $key=>$val ) { $x = $test[$key]['a4']; } // 0.17695760726929
}
$endtime[6][$j] = microtime(true)-$time;
echo $endtime[6][$j].', ';
$formode[6] = 'foreach ($test as $key=>$val ) { $x = $test[$key][\'a4\']; }';

//~~~~~~~~~~~~~~~~~~~~~~
$time = microtime(true);
for($i=0; $i<100000; $i++) {
$keys = array_keys($test);
foreach ($keys as $key) { $x = $test[$key]['a4']; } // 0.17695760726929
}
$endtime[7][$j] = microtime(true)-$time;
echo $endtime[7][$j].', ';
$formode[7] = ' $keys = array_keys($test); foreach ($keys as $key) { $x = $test[$key][\'a4\']; }';

//~~~~~~~~~~~~~~~~~~~~~~
$time = microtime(true);
for($i=0; $i<100000; $i++) {
foreach ($test as $val) { extract($val); $x = $a4; }
}
$endtime[8][$j] = microtime(true)-$time;
echo $endtime[8][$j].', ';
$formode[8] = 'foreach ($test as $val) { $extract($val); $x = $a4; } // Using Extract;

echo '<hr />';
}

echo '<br />';

$top = [];
foreach ($endtime as $key1=>$values) {
$speed = array_sum($values)/count($values);
$top[$key1] = number_format((float)$speed, 5, '.', ''); //~~~ round with fixed 5 decimals
}
asort($top);
?>

<h1 style="text-align: center;">PHP Foreach Loop Benchmark</h1>
<style> ol li {border: 1px solid red; padding: 5px; margin: 5px; font-size: 20px; font-family: monospace;"}</style>
<ol>

<?php
$speed0 = current($top);
foreach ($top as $key=>$speed) {
$slow = round($speed / $speed0, 2);
$slowtext = ($slow != 1) ? "$slow x Slower" : ' FASTEST';
echo '<li>'. $speed .' => <b>'. $formode[$key] . '</b> ~ '.$slowtext.'</li>';
}
echo '</ol>';

Leave a Reply

Your email address will not be published. Required fields are marked *