This article introduces a native function of php and its optimized function execution efficiency comparison. it is concluded that the native function of php is not necessarily the most suitable. Today, when I read the Arr class in the kohana source code, I found such a function.
The code is 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 = 10, $ 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 the native function of php can also implement this function, I suddenly thought of a previous remark about php performance optimization-PHP provided so many native functions for us, and we tried to use native functions to solve the problem. So I did a test to see how fast php native function performance is. The function to be tested includes the native function range () and the above function _ range (). The underline begins with the error "Fatal error: cannot redeclare range () in ".
The code is as follows:
Function _ range ($ step = 10, $ 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 (bytes 00, 3 );
// $ Tmp = _ range (0, 00, 3 );
$ Time ['end'] = microtime (true );
Echo $ time ['end']-$ time ['begin'].'s '. "\ r ";
Echo (memory_get_peak_usage ()/1024/1024). "M ";
Test with native and user-defined functions, respectively ~ When the multiples of all 3 between 1000000, the result was unexpected:
The first is the result of using native functions:
The following is the result of using a custom function:
To make the results more accurate, I am making a chart statistics
Count native function range () custom function _ range ()
(0, 00, 3) 5.155E-3 s 27.5530 M 1.907E-5 s 0.1241 M
(40.2688, 2) 7.479E-3 s 0.1241 M 1.811E-5 s M
(0.1620, 1) 8.16E-5 s 0.1241 M 2.649E-5 s M
From the table, we can see that the user-defined function saves memory and time when a random number is generated, and the native function consumes memory and time when a large number of random numbers are generated, in this regard, user-defined functions are doing well, and the memory and time consumed are basically stable. it seems that what the predecessor mentioned earlier may not be completely correct, however, it should be noted that the custom function here can only generate numbers, while the native range can also generate letters, but I think it is not too difficult to add letters to the custom function ~
It seems that kohana officially knows the range function very well and the complexity of the function in the php kernel is also very familiar, so this small optimization can be done so well, so it's amazing !!!