Skip to content

Instantly share code, notes, and snippets.

@dshafik
Created November 4, 2011 15:41
Show Gist options
  • Save dshafik/1339623 to your computer and use it in GitHub Desktop.
Save dshafik/1339623 to your computer and use it in GitHub Desktop.
<?php
for ($j = 1; $j < 10000000; $j = $j) {
$size = $j;
echo $size . " elements" . PHP_EOL;
$start = microtime(true);
for($i = 0; $i < $size; $i++) {
// do nothing
$array[$i] = new stdClass();
}
for($i = 0; $i < $size; $i++) {
// do nothing
$value = $array[$i];
}
$end = microtime(true);
$time = (($end - $start));
echo "Regular Array: " .$time. "s" . PHP_EOL;
unset($array);
$start = microtime(true);
$fixedArray = new SplFixedArray($size);
for($i = 0; $i < $size; $i++) {
// do nothing
$fixedArray[$i] = new stdClass();
}
$start = microtime(true);
for($i = 0; $i < $size; $i++) {
// do nothing
$value = $fixedArray[$i];
}
$end = microtime(true);
$time2 = (($end - $start));
echo "Fixed Array: " .$time2. "s" . PHP_EOL;
echo "Factor: " . (($time*1000000)/($time2*1000000)) . PHP_EOL;
$j *= 10;
unset($array); unset($fixedArray);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment