PHP Program accelerated exploration of code optimization _php Tutorial

Source: Internet
Author: User
Tags benchmark pear time 0
With Pear::benchmark, you now know how to test your code, how to determine if your code is fast or slow, and which part is slower. So what I'm going to say next is how to eliminate or optimize that part of the slow code.

This point on my personal most important experience is only two points, one is to eliminate the wrong or inefficient cycle, and the second is to optimize the database query statements. In fact, there are some other optimization details, such as "Str_replace faster than Ereg_replace", "Echo is faster than print" and so on. I put this aside for the time being, and later I'll mention using caching to deal with too-frequent IO.

Here we compare the efficiency (time spent) of the function with the same three functions, but the program is written in a different way.

badloops.php

Require_once (' benchmark/iterate.php ');
Define (' Max_run ', 100);
$data = Array (1, 2, 3, 4, 5);

Dobenchmark (' v1 ', $data);
Dobenchmark (' v2 ', $data);
Dobenchmark (' v3 ', $data);
function Dobenchmark ($functionName = null, $arr = NULL)
{
Reset ($arr);
$benchmark = new Benchmark_iterate;
$benchmark->run (Max_run, $functionName, $arr);
$result = $benchmark->get ();
Echo '
';
printf ("%s ran%d times where average exec time%.5f MS", $functionName, $result [' iterations '], $result [' mean '] * 1000);
}

function V1 ($myArray = null) {
A very poor cycle of efficiency
for ($i =0; $i < sizeof ($myArray); $i)
{
Echo ' ';
}
}

Function v2 ($myArray = null) {
Slightly improved efficiency
$max = sizeof ($myArray);
for ($i =0; $i < $max; $i)
{
Echo ' ';
}
}

function v3 ($myArray = null) {
Optimum efficiency
echo " ";
}

?>

The result of the program output is probably this:

V1 ran the times where average exec time 0.18400 ms
V2 ran the times where average exec time 0.15500 ms
V3 ran the times where average exec time 0.09100 ms

As you can see, the execution time of the function becomes less and the efficiency increases.

The function v1 has an obvious error, and each time the loop is called, the sizeof () function needs to be evaluated. The function v2 the number of elements of the $myarray array into the $max variable outside of the loop, eliminating the need to compute the number of elements in the array for Each loop, thus increasing efficiency. Function V3 is the most efficient, using out-of-the-box functions to avoid loops.

This example just gives you a perceptual familiarity with understanding what is relatively efficient code. In actual development, I believe there will be a lot of people will be vague to write a lot of inefficient code. To write the code in a refined and efficient way, I'm afraid it will take time to temper:-) But this is another topic, we skip the talk.

database application basically every PHP program will be used, in the actual development I found that the most impact on the overall system efficiency is the database this part.


http://www.bkjia.com/PHPjc/445122.html www.bkjia.com true http://www.bkjia.com/PHPjc/445122.html techarticle With Pear::benchmark, you now know how to test your code, how to determine if your code is fast or slow, and which part is slower. So the next thing I'm going to say is ...

  • 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.