Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
PHP Benchmark of loop functions
<?php
$a = array();
for ($i = 0; $i < 100000; $i++) {
$a[] = $i;
}
$start = microtime(true);
foreach ($a as $k => $v) {
$a[$k] = $v + 1;
}
echo "1- Completed in ", microtime(true) - $start, " Seconds\n";
$start = microtime(true);
foreach ($a as $k => &$v) {
$v = $v + 1;
}
echo "2- Completed in ", microtime(true) - $start, " Seconds\n";
$start = microtime(true);
foreach ($a as $k => $v) {}
echo "3- Completed in ", microtime(true) - $start, " Seconds\n";
$start = microtime(true);
foreach ($a as $k => &$v) {}
echo "4- Completed in ", microtime(true) - $start, " Seconds\n";
$start = microtime(true);
$countFor = count($a);
for ($i=0;$i<$countFor;$i++) {
$a[$i] = $a[$i]+1;
}
echo "5- Completed in ", microtime(true) - $start, " Seconds\n";
$start = microtime(true);
$countFor = count($a);
for ($i=0;$i<=$countFor;$i++) {}
echo "6- Completed in ", microtime(true) - $start, " Seconds\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment