Skip to content

Instantly share code, notes, and snippets.

@datibbaw
Created March 14, 2013 16:58
Show Gist options
  • Save datibbaw/5163048 to your computer and use it in GitHub Desktop.
Save datibbaw/5163048 to your computer and use it in GitHub Desktop.
<?php
function array1($max)
{
$a = array();
for ($i = 0; $i < $max; ++$i) {
$a[$i] = $i;
}
for ($i = 0; $i < $max; ++$i) {
$i = $a[$i];
}
}
function splarray1($max)
{
$a = new SplFixedArray($max);
for ($i = 0; $i < $max; ++$i) {
$a[$i] = $i;
}
for ($i = 0; $i < $max; ++$i) {
$i = $a[$i];
}
}
function testall($max, $n, $desc)
{
echo "Test case: $desc ($n iterations)\n";
$results = array();
$start = microtime(true);
for ($i = 0; $i != $n; ++$i) {
array1($max);
}
$results['array1'] = microtime(true) - $start;
$start = microtime(true);
for ($i = 0; $i != $n; ++$i) {
splarray1($max);
}
$results['splarray1'] = microtime(true) - $start;
showresults($results);
}
function showresults(array $results)
{
$min = PHP_INT_MAX;
$minkey = -1;
foreach ($results as $key => $time) {
if ($time < $min) {
$min = $time;
$minkey = $key;
}
}
foreach ($results as $key => $time) {
echo sprintf("%-20s%-10.5f %s\n", $key, $time, $key === $minkey ? '(fastest)' : '-' . number_format(100 * ($time - $min) / $min, 2) . '%');
}
}
testall(1000, 20000, 'Size 1000');
testall(100, 20000, 'Size 100');
testall(500, 20000, 'Size 500');
testall(2000, 20000, 'Size 2000');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment