Try to use PHP native functions ?, Php function _ PHP Tutorial

Source: Internet
Author: User
Tags kohana
Try to use PHP native functions ?, Php functions. Try to use PHP native functions ?, When I read the Arr class in the kohana source code, the php function found this function *** Fillanarraywitharangeofnumbers. ** Filla tries to use PHP native functions as much as possible ?, Php functions

Today, when I read the Arr class in the kohana source code, I found such a function.

/** * 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 php's original sound function can also implement this function, I suddenly thought of a previous remark about php performance optimization-PHP provided so many original sound functions for us. 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 native function range () and the above function _ range () are used to test the function. here, the underline is used to indicate that the error "Fatal error: cannot redeclare range () in ".
        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(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";

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:

CountNative function range ()Custom Function _ range ()(, 3)5.155E-3 s27.5530 M1.907E-5 s0.1241 M(, 2)7.479E-3 s40.2688 M1.811E-5 s0.1241 M(0,1000, 1)8.16E-5 s0.1620 M2.649E-5 s0.1241 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 !!!

Send Me ~

Why ?, The php function found this function when reading the Arr class in the kohana source code/*** Fill an array with a range of numbers. ** // Fill...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.