Tips: How to optimize PHP website program code

Source: Internet
Author: User
Tags time 0
How can I eliminate or optimize the PHP code for developing website programs? In this regard, my personal most important experience is only two points. one is to eliminate errors or inefficient loops, and the other is to optimize database query statements. There are some other optimization details, such

How can I eliminate or optimize the PHP code for developing website programs?

In this regard, my personal most important experience is only two points. one is to eliminate errors 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 (time-consuming) of the three functions with the same functions but different programming languages.

Webjx. 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 of the program is probably like this:

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

We can see that the function is executed less time, and the efficiency increases.

Function v1 has a very obvious error. the time of each loop needs to be calculated by calling the sizeof () function. 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 trust that many people will write a lot of inefficient code in a vague manner. I am afraid it may take time to refine the code:-) but this is another topic. let's skip it.

Each PHP program is used based on database utilization. in actual development, the most influential part of the invention is the database. Database optimization and data query statement optimization are not discussed in detail.

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.