PHP program to speed up the exploration of code optimization

Source: Internet
Author: User
Tags array benchmark functions mysql query query time 0 mysql query optimization
Program | optimization mastered Pear::benchmark, now that you know how to test your code, know how to determine if your code is fast or slow, which part is slower. So the next thing I want to say is how to eliminate or optimize that part of the slow code.





this point my personal most important experience is only two points, one is to eliminate the wrong or inefficient cycle; the second is to optimize the database query statement. 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'll put these aside for a while, and I'll mention using caching to deal with too-frequent IO.





below we will compare the efficiency (time of consumption) of three functions with the same function, but the program is written differently.





badloops.php





<?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 ' <br> ';


printf ("%s ran%d times where average exec time%.5f MS", $functionName, $result [' iterations '], $result [' mean '] * 1000) ;


}





function v1 ($myArray = null) {


//inefficient cycle


for ($i =0; $i sizeof ($myArray); $i + +)


 {


echo '!--'. $myArray [$i]. '--> ';


 }


}








function v2 ($myArray = null) {


//efficiency is slightly improved


$max = sizeof ($myArray);


for ($i =0; $i $max; $i + +)


 {


echo '!--'. $myArray [$i]. '--> ';


 }


}





function v3 ($myArray = null) {


//Best Efficiency


echo "!--", Implode ("-->!--", $myArray), "-->";


}





? >

The result of the
program output is probably this:





V1 ran where average exec time 0.18400 Ms


v2 ran where average exec time 0.15500 Ms


v3 ran where average exec time 0.09100 Ms





can see that the function has less execution time and higher efficiency.





function V1 has a very obvious error, each time of the loop, you need to call the sizeof () function to calculate. The function v2 the number of elements in the $myarray array outside the loop to the $max variable, avoiding the need to compute the number of elements of the array for Each loop, so the efficiency is increased. Function V3 is the most efficient, using ready-made functions to avoid loops.





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





database application basically every PHP program will be used, in the actual development I found that the most affect the efficiency of the entire system is the database part. As for the optimization of database and the optimization of data query statement, this is limited to the length of the discussion. Readers can refer to the "MySQL Query optimization technology lectures"





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.