- array_sum: documentation ( source)
- microtime: documentation ( source)
- array_slice: documentation ( source)
- end: documentation ( source)
- array_key_last: documentation ( source)
- array_fill: documentation ( source)
- key: documentation ( source)
<?php
function returnTime(callable $function, int $repeat = 100)
{
$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, 500000, 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);
});
This script was stopped while abusing our resources