Use PHP native functions whenever possible? , PHP functions
Today, when I read the Arr class in Kohana source, I discovered a function like this
/** * Fill An array with a range of numbers. * * //Fill an array with values 5, * $values = Arr::range (5, +); * * @param integer $step ste pping * @param integer $max ending number * @return array */public static function range ($step = ten, $max = 10 0) {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 original function of PHP can also realize this function, and suddenly think of a predecessor who had heard about the performance optimization of PHP--php to provide us with so many acoustic 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 native function range () and the above function _range (), where the underscore begins because the overwrite of the acoustic function range () will error "Fatal Error:cannot redeclare range () in".
function _range ($step = ten, $max = +) {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:
Count native function range () Custom function _range () (0,1000000,3) 5.155e-3s27.5530m1.907e-5s0.1241m (0,1000000,2) 7.479e-3s40.2688m1.811e-5s0.1241m (0,1000,1) 8.16e-5s0.1620m2.649e-5s0.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!!!
Send me~
http://www.bkjia.com/PHPjc/923414.html www.bkjia.com true http://www.bkjia.com/PHPjc/923414.html techarticle use PHP native functions whenever possible? , the PHP function found a function/** * Fill An array with a range of numbers when reading the Arr class in Kohana source today. * *//Fill a ...