note 112138 added to function.key
| From: | B.Keil | Date: | Wed, 08 May 2013 23:19:37 +0000 |
| Subject: | note 112138 added to function.key | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-194746@lists.php.net to get a copy of this message | ||
@FatBat
Your solution is elegant in that it is swift and short - only two commands.
However, performance wise it's not ideal to sort an array to get the key of the highest value.
I have written a test case with three examples - yours, another, sophisticated function that allows
to pass your own compare function and a third, optimized for performance.
<?php
error_reporting(E_ALL|E_STRICT);
function key_of_max_val(array $array, $cmpFunction = null) {
if ($cmpFunction === null) $cmpFunction = create_function('$a,$b', 'return $a
> $b;');
if (!function_exists($cmpFunction)) {
trigger_error(
is_string($cmpFunction) ?
"Did not find function '".mb_substr($cmpFunction, 0,
20)."'" :
"String reference to function expected for
\$cmpFunction\n",
E_USER_WARNING
);
return null;
}
$maxKey = key($array);
foreach ($array as $key => $value)
if ($cmpFunction($value, $array[$maxKey]) > 0)
$maxKey = $key;
return $maxKey;
}
function key_of_max_val2(array $array) {
$maxVal = reset($array);
$maxKey = key($array);
foreach ($array as $key => $value) {
if ($value > $maxVal) {
$maxKey = $key;
$maxVal = $value;
}
}
return $maxKey;
}
function key_of_max_val3(array $array) {
arsort($array);
return key($array);
}
$array = array('nadine', 'Natasha', 'Nina', 'nicole');
echo $array[key_of_max_val($array)]."\n";
echo $array[key_of_max_val($array, 'strcasecmp')]."\n";
echo $array[key_of_max_val2($array)]."\n";
echo $array[key_of_max_val3($array)]."\n";
$array = array();
for($i = 0; $i < 1000000; $i++) $array[] = rand(0, 1024*1024);
foreach(array('key_of_max_val', 'key_of_max_val2','key_of_max_val3')
as $function) {
$before = microtime(true);
$key = $function($array);
$after = microtime(true);
printf("%s returned key %d referring to %d after %.2f seconds.\n", $function,
$key, $array[$key], $after - $before);
}
?>
Results:
$ php example.php
nicole
Nina
nicole
nicole
key_of_max_val returned key 360688 referring to 1048575 after 0.87 seconds.
key_of_max_val2 returned key 360688 referring to 1048575 after 0.16 seconds.
key_of_max_val3 returned key 360688 referring to 1048575 after 1.65 seconds.
However, function 2 has the disadvantage of actually changing the internal array pointer, since
it's call-by-reference. If you do that in a foreach loop over $array you're in trouble.
Without the reference a typical result is 0.25 seconds.
----
Server IP: 69.147.83.199
Probable Submitter: 77.10.21.106
----
Manual Page -- http://php.net/manual/en/function.key.php
Edit -- https://master.php.net/note/edit/112138
Del: integrated -- https://master.php.net/note/delete/112138/integrated
Del: useless -- https://master.php.net/note/delete/112138/useless
Del: bad code -- https://master.php.net/note/delete/112138/bad+code
Del: spam -- https://master.php.net/note/delete/112138/spam
Del: non-english -- https://master.php.net/note/delete/112138/non-english
Del: in docs -- https://master.php.net/note/delete/112138/in+docs
Del: other reasons-- https://master.php.net/note/delete/112138
Reject -- https://master.php.net/note/reject/112138
Search -- https://master.php.net/manage/user-notes.php