PEAR: BenchMark. now you know how to test your code and how to judge which part of your code is slow. Next I will talk about how to eliminate or optimize the slow code. In this regard, my main experience is only two points. one is to eliminate incorrect or inefficient SyntaxHighlighter. all (); Master PEAR: BenchMark. now you know how to test your code and how to judge which part of your code is slow. Next I will talk about how to eliminate or optimize the slow code.
In this regard, my main experience is only two points. one is to eliminate incorrect or inefficient loops, and the other is to optimize database query statements. There are some other optimization details, such as "str_replace faster than ereg_replace" and "echo faster than print. These are all put aside for the moment. later I will mention using cache to deal with too frequent IO.
Next we will compare the efficiency (consumed time) of the three functions with the same functions but different programming languages.
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 ){ // Cycle with poor efficiency For ($ I = 0; $ I <sizeof ($ myArray); $ I ++) { Echo <! --. $ MyArray [$ I]. -->; } } Function v2 ($ myArray = null ){ // The efficiency is slightly improved $ Max = sizeof ($ myArray ); For ($ I = 0; $ I <$ max; $ I ++) { Echo <! --. $ MyArray [$ I]. -->; } } Function v3 ($ myArray = null ){ // Optimal Efficiency Echo "<! -- ", Implode (" --> <! -- ", $ MyArray)," --> "; } ?> |
The output result of the program is as follows:
V1 ran 100 times where average exec time 0.18400 MS
V2 ran 100 times where average exec time 0.15500 MS
V3 ran 100 times where average exec time 0.09100 MS
As you can see, the function execution time decreases and the efficiency increases.
Function v1 has a very obvious error. the sizeof () function must be called to calculate each cycle time. Function v2 saves the number of elements in the $ myArray array to the $ max variable outside the loop, avoiding the need to calculate the number of elements in the array in each loop, so the efficiency is improved. Function v3 is the most efficient and uses ready-made functions to avoid loops.
This example only gives you a perceptual understanding of what is relatively efficient code. In actual development, I believe many people will write a lot of inefficient code. I'm afraid it will take time to refine the code:-) but this is another topic. let's skip it.
Database applications are basically used by every PHP program. in actual development, I found that the most influential part of the system efficiency is the database. Database optimization and data query statement optimization are not discussed in detail. Readers can refer to the MySQL Query optimization technical lecture