Skip to content

Instantly share code, notes, and snippets.

@fijiwebdesign
Created November 1, 2014 19:45
Show Gist options
  • Save fijiwebdesign/eed9f4f85e7f9d1e14b5 to your computer and use it in GitHub Desktop.
Save fijiwebdesign/eed9f4f85e7f9d1e14b5 to your computer and use it in GitHub Desktop.
Is Associative Array or Indexed Array Benchmarks.
<?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";
}
@fijiwebdesign
Copy link
Author

System:

Microsoft Windows [Version 6.1.7601] Intel64 Family 6 Model 58 Stepping 9 GenuineIntel ~2601 Mhz

PHP 5.5.12 (cli) (built: Apr 30 2014 11:20:58)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans

Results:

Testing method1 (array_values check) - 1000 iterations
  Total time: 0.39100 s
  Average   : 0.02607 ms / test

Testing method2 (array_keys comparison) - 1000 iterations
  Total time: 0.57720 s
  Average   : 0.03848 ms / test

Testing method3 (array_filter of keys) - 1000 iterations
  Total time: 6.00601 s
  Average   : 0.40040 ms / test

Testing method4 (foreach typecast) - 1000 iterations
  Total time: 0.54600 s
  Average   : 0.03640 ms / test

Testing method5 (foreach typecheck) - 1000 iterations
  Total time: 5.80421 s
  Average   : 0.38695 ms / test

Testing method6 (range array_keys) - 1000 iterations
  Total time: 0.65520 s
  Average   : 0.04368 ms / test

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