- array_sum: documentation ( source)
- microtime: documentation ( source)
- array_values: documentation ( source)
- range: documentation ( source)
<?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 = range(0, 450000);
unset($array[0]);
echo "Duration of array_values: ", returnTime(function() use ($array) {
$newArray = array_values($array);
});
echo PHP_EOL;
echo "Duration of splat-pack: " , returnTime(function() use ($array){
$newArray = [...$array];
});