@ 2019-07-30T23:16:09Z <?php
function returnTime(callable $function, int $repeat = 50)
{
$tests = [];
for ($i = 0; $i < $repeat; ++$i) {
$startTime = microtime(true);
$function();
$endTime = microtime(true);
$tests[] = $endTime - $startTime;
}
// Representing the average
return 1000 * array_sum($tests) / $repeat;
}
$array = array_fill(0, 200000, 1);
echo "Duration of array_slice() + key(): ", returnTime(function() use ($array) {
$lastKey = key(array_slice($array, -1, 1, true));
});
echo PHP_EOL;
echo "Duration of end() + key(): " , returnTime(function() use ($array){
end($array);
$lastKey = key($array);
});
echo PHP_EOL;
echo "Duration of array_key_last(): " , returnTime(function() use ($array){
$lastKey = array_key_last($array);
});
echo PHP_EOL;
echo "Duration of array_keys() + end(): " , returnTime(function() use ($array){
$keys = array_keys($array);
$last = end($keys);
});
echo PHP_EOL;
echo "Duration of array_reverse() + key(): " , returnTime(function() use ($array){
$key = key(array_reverse($array));
});
echo PHP_EOL;
echo "Duration of array_slice() + foreach(): " , returnTime(function() use ($array){
foreach (array_slice($array, -1, 1, true) as $key => $value);
});
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.2.9 0.259 0.070 23.71 7.3.7 0.539 0.260 26.71 7.3.6 0.352 0.316 26.96 7.3.5 0.521 0.289 26.82 7.3.4 0.507 0.265 26.82 7.3.3 0.487 0.245 26.48 7.3.2 0.102 0.253 32.29 7.3.1 0.096 0.274 32.45 7.3.0 0.149 0.258 32.51 7.2.20 0.174 0.120 27.20 7.2.19 0.160 0.113 27.11 7.2.18 0.191 0.087 27.07 7.2.17 0.179 0.100 26.85 7.2.16 0.189 0.116 27.03 7.2.15 0.042 0.082 32.48 7.2.14 0.058 0.105 32.87 7.2.13 0.050 0.123 32.70 7.2.12 0.043 0.113 32.68 7.2.11 0.038 0.099 32.84 7.2.10 0.053 0.100 32.68 7.2.9 0.033 0.107 32.60 7.2.8 0.044 0.105 32.75 7.2.7 0.052 0.100 32.64 7.2.6 0.037 0.096 32.73 7.2.5 0.054 0.080 32.96 7.2.4 0.027 0.106 32.54 7.2.3 0.048 0.092 32.43 7.2.2 0.028 0.115 32.34 7.2.1 0.042 0.091 32.70 7.2.0 0.050 0.109 32.51 7.1.30 0.689 0.107 29.45 7.1.29 0.256 0.090 31.72 7.1.28 0.049 0.096 31.88 7.1.27 0.055 0.104 31.59 7.1.26 0.044 0.109 31.76 7.1.25 0.045 0.094 31.36
preferences:dark mode live preview
32.26 ms | 403 KiB | 5 Q