Skip to content

Instantly share code, notes, and snippets.

@dshafik
Created August 11, 2011 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dshafik/1138794 to your computer and use it in GitHub Desktop.
Save dshafik/1138794 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