Created
November 1, 2014 19:45
-
-
Save fijiwebdesign/eed9f4f85e7f9d1e14b5 to your computer and use it in GitHub Desktop.
Is Associative Array or Indexed Array Benchmarks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// usage | |
if(!isset($argv[1])){ | |
echo "\nUsage: ".$argv[0]." [options] iterations | |
Required: | |
iterations - Number of iterations of each test | |
Example: | |
php $argv[0] -v 1000 | |
Options: | |
-v | |
--verbose | |
Show verbose information. Return status for single iteration of each test. | |
"; | |
exit(1); | |
} | |
// command line options | |
$options = getopt('v', array('verbose')); | |
$verbose = isset($options['v']) || isset($options['verbose']); | |
/** | |
* Arrays to check | |
*/ | |
$tests = array( | |
array(array( | |
'foo' => 'bar', | |
)), | |
array(array( | |
'bar', | |
'foo' => 'bar', | |
'baz', | |
)), | |
array(null), | |
array(true), | |
array(false), | |
array(0), | |
array(1), | |
array(0.0), | |
array(1.0), | |
array('string'), | |
array(array(0, 1, 2)), | |
array(new stdClass), | |
array_fill(0,1000,uniqid()), // big numeric array | |
array_fill_keys(range(2,1000,3),uniqid()), // big misaligned numeric array (=associative) | |
array_fill_keys( // big associative array | |
str_split( | |
str_repeat(uniqid('',true),100), | |
3 | |
), | |
true | |
) | |
); | |
$iterations = (int)$argv[1]; | |
/** | |
* Common methods to check associative array | |
*/ | |
$methods = array( | |
'method1 (array_values check)' => | |
function($array){ | |
return (array_values($array) !== $array); | |
}, | |
'method2 (array_keys comparison)' => | |
function($array){ | |
$array = array_keys($array); return ($array !== array_keys($array)); | |
}, | |
'method3 (array_filter of keys)' => | |
function($array){ | |
return count(array_filter(array_keys($array), 'is_string')) > 0; | |
}, | |
'method4 (foreach typecast)' => | |
function($array){ | |
foreach($array as $key => $value) { | |
if ($key !== (int) $key) { | |
return true; | |
} | |
} | |
return false; | |
}, | |
'method5 (foreach typecheck)' => | |
function($array){ | |
foreach($array as $key => $value) { | |
if (!is_int($key)) { | |
return true; | |
} | |
} | |
return false; | |
}, | |
'method6 (range array_keys)' => | |
function($array){ | |
return $array === array() || range(0, count($array)-1) === array_keys($array); | |
}, | |
); | |
/** | |
* Perform benchmark on each method | |
*/ | |
foreach($methods as $name=>$func){ | |
echo "Testing $name - $iterations iterations\n"; | |
$time = microtime(true); | |
// show test results | |
if ($verbose) { // verbose | |
foreach($tests as $array){ | |
var_dump($func($array)); | |
} | |
} | |
// show test benchmarks | |
for($x=0;$x<$iterations;$x++){ | |
foreach($tests as $array){ | |
($func($array)); | |
} | |
} | |
/** | |
* Show results | |
*/ | |
$totalTime = (microtime(true) - $time); | |
$avgTime = @($totalTime / ($iterations * count($tests))); | |
echo " Total time: ".number_format($totalTime,5,'.',' ')." s\n"; | |
echo " Average : ".number_format($avgTime*1000,5,'.',' ')." ms / test \n"; | |
echo "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
System:
Results: