PHP program accelerated exploration Code Optimization _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags time 0
Code Optimization for PHP program accelerated exploration. Grasp PEAR: BenchMark. now you know how to test your code and how to determine which part of your code is slow. Next, I want to talk about PEAR: BenchMark. now you know how to test your code and how to judge whether your code is fast or slow, which part 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

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 ){
// Cycle with poor efficiency
For ($ I = 0; $ I <sizeof ($ myArray); $ I)
{
Echo' ';
}
}

Function v2 ($ myArray = null ){
// The efficiency is slightly improved
$ Max = sizeof ($ myArray );
For ($ I = 0; $ I <$ max; $ I)
{
Echo' ';
}
}

Function v3 ($ myArray = null ){
// Optimal Efficiency
Echo" ";
}

?>

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 is just to give you a perceptual knowledge and understand what is relatively efficient code. In actual development, I believe many people will write a lot of inefficient code in a vague manner. 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.


Bytes. So what I want to talk about next 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.