PHP native function must be good? , PHP native function
Today, when I read the Arr class in Kohana source, I discovered a function like this
Copy the Code code as follows:
/**
* Fill an array with a range of numbers.
*
*//Fill an array with values 5, 10, 15, 20
* $values = Arr::range (5, 20);
*
* @param integer $step Stepping
* @param integer $max ending number
* @return Array
*/
public static function range ($step = ten, $max = 100)
{
if ($step < 1)
return Array ();
$array = Array ();
for ($i = $step; $i <= $max; $i + = $step)
{
$array [$i] = $i;
}
return $array;
}
When I saw this, I found that PHP's native function was also able to implement this function, and suddenly thought of a predecessor who had heard about PHP performance optimization--php to provide us with so many native functions, we try to solve the problem with the native function. So I did a test to see if PHP native function performance was much faster than what I had written. The function to be tested has the original function range () and the above function _range (), where the underscore begins because overriding the native function range () will error "Fatal Error:cannot redeclare range () in".
Copy the Code code as follows:
function _range ($step = ten, $max = 100)
{
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 = _range (0,1000000,3);
$time [' end '] = Microtime (true);
echo $time [' End ']-$time [' Begin ']. ' S '. " \ r ";
Echo (Memory_get_peak_usage ()/1024/1024). " M ";
Testing with native functions and custom functions, respectively, resulted in a multiple of 3 between 0~1000000, which was unexpected to me:
The first is the result of using the native function:
The following is the result of using a custom function:
For the sake of accuracy, I'm doing a chart statistic.
Count native function range () Custom function _range ()
(0,1000000,3) 5.155e-3s 27.5530M 1.907e-5s 0.1241M
(0,1000000,2) 7.479e-3s 40.2688M 1.811e-5s 0.1241M
(0,1000,1) 8.16e-5s 0.1620M 2.649e-5s 0.1241M
It can be seen from the table that the custom function saves memory and time when the random number is generated, and the native function consumes memory especially when generating a large number of random numbers, and the custom function behaves well in this respect, and the memory and time consumed are basically stable. It seems that the previous predecessor said is not necessarily completely correct oh, but it is important to note that our custom function here can only generate numbers, and the original range can also produce letters, but I think this custom function to add a letter should not be too difficult ~
It seems that Kohana official to range This function is very understanding, the PHP kernel in the complexity of the function is also very understanding, so this small optimization can do so well, too!!!
http://www.bkjia.com/PHPjc/923904.html www.bkjia.com true http://www.bkjia.com/PHPjc/923904.html techarticle PHP native function must be good? , PHP native functions today when reading the Arr class in the Kohana source code, found a function to copy code as follows:/** * Fill an array with a R ...