Created
August 22, 2012 18:35
-
-
Save morrisonlevi/3428205 to your computer and use it in GitHub Desktop.
Performance: array_max_recursive implementations
This file contains 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 | |
if (count($argv) > 1) { | |
define('NEST_LEVEL', $argv[1]); | |
define('NUMBER_OF_TEST_ITERATIONS', $argv[2]); | |
} else { | |
define('NEST_LEVEL', 50); | |
define('NUMBER_OF_TEST_ITERATIONS', 5); | |
} | |
function MaxArrayA(array $array) { | |
$max = NULL; | |
$stack = array($array); | |
do { | |
$current = array_pop($stack ); | |
foreach ($current as $value) { | |
if (is_array($value)) { | |
$stack[] = $value; | |
} elseif (filter_var($value, FILTER_VALIDATE_INT) !== FALSE) { | |
// max(NULL, 0) returns NULL, so cast it | |
$max = (int) max($max, $value); | |
} | |
} | |
} while (!empty($stack)); | |
return $max; | |
} | |
function MaxArrayB($arr) { | |
$max = NULL; | |
array_walk_recursive($arr, function ($current) use (&$max) { | |
if (filter_var($current, FILTER_VALIDATE_INT) !== FALSE) { | |
$max = max($max, $current); | |
} | |
}); | |
return $max; | |
} | |
function MaxArrayC($arr) { | |
$max = FALSE; | |
array_walk_recursive($arr, function ($current) use (&$max) { | |
if (is_int($current) && ($current > $max || $max === FALSE)) { | |
$max = $current; | |
} | |
}); | |
return $max; | |
} | |
function MaxArrayD($arr) { | |
$max = FALSE; | |
foreach ($arr as $value) { | |
if (is_array($value)) { | |
$max = max($max, MaxArrayD($value)); | |
} elseif (filter_var($value, FILTER_VALIDATE_INT) !== FALSE) { | |
// max(NULL, 0) returns NULL, so cast it | |
$max = (int) max($max, $value); | |
} | |
} | |
return $max; | |
} | |
function MaxArrayE($arr) { | |
$max = FALSE; | |
foreach ($arr as $value) { | |
if (is_array($value)) { | |
$max = max($max, MaxArrayE($value)); | |
} elseif (is_int($value)) { | |
// max(NULL, 0) returns NULL, so cast it | |
$max = (int) max($max, $value); | |
} | |
} | |
return $max; | |
} | |
$heavilyNestedArray = array(1); | |
for ($i = 1; $i < NEST_LEVEL; $i++) { | |
$heavilyNestedArray[] = $i; | |
$heavilyNestedArray[] = $heavilyNestedArray; | |
} | |
function testFunction($callable, $array) { | |
$startTime = microtime(TRUE); | |
$callable($array); | |
$timeTaken = microtime(TRUE) - $startTime; | |
return $timeTaken; | |
} | |
$results = array( | |
'a' => array(), | |
'b' => array(), | |
'c' => array(), | |
'd' => array(), | |
'e' => array(), | |
); | |
for ($i = 0; $i < NUMBER_OF_TEST_ITERATIONS; $i++) { | |
$results['a'][] = testFunction('MaxArrayA', $heavilyNestedArray); | |
$results['b'][] = testFunction('MaxArrayB', $heavilyNestedArray); | |
$results['c'][] = testFunction('MaxArrayC', $heavilyNestedArray); | |
$results['d'][] = testFunction('MaxArrayD', $heavilyNestedArray); | |
$results['e'][] = testFunction('MaxArrayE', $heavilyNestedArray); | |
} | |
$print_result = function ($result, $name) { | |
echo "Function '$name' at nest level ", NEST_LEVEL, ' and ' , NUMBER_OF_TEST_ITERATIONS , " iterations:\n"; | |
echo "Min:" . min($result) . "\n"; | |
echo "Max:" . max($result) . "\n"; | |
echo "Average:" . (array_sum($result)/count($result)) . "\n"; | |
echo str_repeat('=', 40), "\n"; | |
}; | |
echo str_repeat('=', 40), "\n"; | |
array_walk($results, $print_result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment