From the Internet, there are times when the efficiency of native functions may not be as high as a custom function
For example, the range function, used to initialize a key value and key name equal, and the key name difference value of the same array, the efficiency of the original function is not high,
This may be because the range function does not only implement this requirement, it can also accept some other parameters to execute the algorithm, the return value
So when we just need a feature, we might use our own function to run faster, and the native function passes a variety of judgments before it can execute the method you want (you might think it's wrong)
Cases:
function ranges ($step = 5, $max = 20)
{
if ($step < 1)
return Array ();
$array = Array ();
for ($i = $step; $i <= $max; $i + = $step)
{
$array [$i] = $i;
}
return $array;
}
$time [' begin '] = Microtime (true);
$tmp = range (0,1000000,3);
$tmp = ranges (0,1000000,3);
$time [' end '] = Microtime (true);
echo $time [' End ']-$time [' Begin ']. ' S '. " \ r ";
Echo (Memory_get_peak_usage ()). " M ";
Custom:
7.8678131103516e-6s 0.59011840820312M
Native
0.069043874740601s 68.172889709473M
PHP custom functions and native function execution efficiency issues