Skip to content

Instantly share code, notes, and snippets.

@borisguery
Created July 18, 2012 07:43
Show Gist options
  • Save borisguery/3134871 to your computer and use it in GitHub Desktop.
Save borisguery/3134871 to your computer and use it in GitHub Desktop.
SplFixedArray vs Native Array lookup performance
<?php
ini_set('memory_limit', -1);
$count = (int)$argv[1];
function fixed_array_search_foreach($stack, $counter, $lookup, $type) {
$start = microtime(true);
foreach ($stack as $key => $value) {
if ($lookup === $value) {
$counter[$key] = 1 + $counter[$key];
break;
}
}
$end = microtime(true);
printf("FixedArray \tForeach\t%s%s\t: %.6f\n", $type, str_repeat(' ', 15-strlen($type)), $end-$start);
return $end-$start;
}
function fixed_array_search_native($stack, $counter, $lookup, $type) {
$start = microtime(true);
if (false !== ($key = array_search($lookup, $stack->toArray()))) {
$counter[$key] = 1 + $counter[$key];
}
$end = microtime(true);
printf("FixedArray \tNative\t%s%s\t: %.6f\n", $type, str_repeat(' ', 15-strlen($type)), $end-$start);
return $end-$start;
}
function array_search_foreach($stack, $lookup, $type) {
$start = microtime(true);
foreach ($stack as $key => $value) {
if ($lookup === $key) {
++$stack[$key];
break;
}
}
$end = microtime(true);
printf("Array \tForeach\t%s%s\t: %.6f\n", $type, str_repeat(' ', 15-strlen($type)), $end-$start);
return $end-$start;
}
function array_search_native($stack, $lookup, $type) {
$start = microtime(true);
if (false !== ($key = array_search($lookup, array_keys($stack)))) {
++$stack[$lookup];
}
$end = microtime(true);
printf("Array \tNative\t%s%s\t: %.6f\n", $type, str_repeat(' ', 15-strlen($type)), $end-$start);
return $end-$start;
}
$fixedArray = new SplFixedArray($count);
$counter = new SplFixedArray($count);
for ($i = 0; $i < $count; ++$i) {
$fixedArray[$i] = rand(0,255) . '.' . rand(0,255) . '.' . rand(0,255) . '.' . rand(0,255);
$counter[$i] = rand(0, 20);
}
fixed_array_search_foreach($fixedArray, $counter, $fixedArray[0], 'first');
fixed_array_search_foreach($fixedArray, $counter, end($fixedArray), 'last');
fixed_array_search_foreach($fixedArray, $counter, 'inexistant', 'inexistant');
fixed_array_search_native($fixedArray, $counter, $fixedArray[0], 'first');
fixed_array_search_native($fixedArray, $counter, end($fixedArray), 'last');
fixed_array_search_native($fixedArray, $counter, 'inexistant', 'inexistant');
unset($fixedArray, $counter, $keys);
$array = array();
for ($i = 0; $i < $count; ++$i) {
$key = rand(0,255) . '.' . rand(0,255) . '.' . rand(0,255) . '.' . rand(0,255);
$array[$key] = rand(0, 20);
}
$keys = array_keys($array);
array_search_foreach($array, $keys[0], 'first');
array_search_foreach($array, end($keys), 'last');
array_search_foreach($array, 'inexistant', 'inexistant');
array_search_native($array, $keys[0], 'first');
array_search_native($array, end($keys), 'last');
array_search_native($array, 'inexistant', 'inexistant');
unset($array, $keys);
@Frikki
Copy link

Frikki commented Jul 30, 2014

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment