PHP custom functions and native function execution efficiency issues

Source: Internet
Author: User

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

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.