Optimizing PHP Programs

Source: Internet
Author: User
Many articles on the web to optimize the PHP program, through the installation of Zend Optimizer, such as accelerated software implementation, but this acceleration is limited. This article mainly starts from the program code to introduce some optimization means.

1, the more abstract layer of the program, the more strict separation of the abstraction layer, the lower the program efficiency.

The most original PHP program used in Web pages is the script embedding mode, which is to embed the PHP script by adding an identifier only in a Web page where the data needs to be dynamically processed or displayed. In general this is the first PHP programmer to learn the model, it has only one layer of abstraction, is the Web page, so this article called it a Single-layer model.

As the scale of the site increases, programmers may find that a single layer of the program is difficult to maintain, when you want to modify or expand the functionality of the program, you will find that the code is very confusing, feel the impossible. So the template class was born, and it made a Web page composed of two files: a PHP program file, an HTML template file. Commonly used template classes have Phplib Library with template template class, Smarty template class, and so on. Due to the addition of additional handlers (template classes), the program efficiency is reduced. If you don't believe it, you can test it yourself. In fact, without a function (the most original programming method) is more efficient than using a function (process-oriented), and the efficiency of the function is higher than the object encapsulation (object oriented). So even in a compiled language, where high efficiency is needed to write in C without C + +, such as the kernel of the FreeBSD operating system, and where the need for extreme efficiency is to be compiled.

In order to enable the program to adapt to a variety of database systems, or to facilitate the conversion of database systems, often a class with the database to deal with the function of encapsulation, so when the conversion database system as long as the package to replace the line, the main program does not need to be modified. Another class was used here, and the efficiency was compromised.

The use of the template class above divides the program into two layers of abstraction: The program layer and the presentation layer. And the use of database class divides the program layer into the interface layer and data processing layer.

The larger the project, the more abstraction layers need to be separated, which makes the Division of labor clear and manageable, but at the expense of program execution efficiency.

For the efficiency degradation caused by the abstraction layer, there are two optimization methods: reducing the abstraction layer and optimizing the interface between the abstraction layers. Generally, the abstraction layer should not be blindly reduced to improve efficiency, which makes code confusing and difficult to manage. But you should not create too many layers of abstraction for small projects unless you have a plan to do it in the future. There is no further discussion on how to properly segment the abstract layer.

For the two layered examples mentioned above, the interface between the optimization abstraction layers is the template class and the database operation class. Abstract layer interfaces need to be called frequently in the program to exchange information between different layers, so the layer interface is well worth optimizing. For database interface classes, it is possible to simply encapsulate some database functions, there is little room for optimization. For template classes, there are many times when there is a greater scope for optimization. Generally, the more generic the template model is, the more powerful the template class function is, and the lower the efficiency, for example, the template class of the Phplib Library has great scope for optimization. and Smarty template analogy phplib template more complex, I have not used, said to have a caching mechanism, I do not know whether it can compensate for its performance loss. Here's a look at how much of the Phplib template class can be tuned out.

(1) When reading the template file, the file function is inefficient and the get_file_content function is replaced.

(2) When matching a child template, the regular expression substitution function is preg_replace inefficient, using the Str_pos function for positioning and replacing with the Str_replace function. This optimization method after the text will be analyzed in detail.

(3) The template model is very versatile and adaptable to all kinds of situations, but the general method efficiency may be very low in the processing of specific details. You can modify the template model as appropriate. My approach is to build a relatively generic template class and then derive a template class that applies only to a particular program. Some optimizations can be made on the template model of the generic template class (relative to the Phplib template), for example, when dealing with a two-dimensional datasheet, it is more complex to use phplib template processing, which requires multiple calls to the class method (essentially a function call). So when rewriting the function of processing two-dimensional data table can be encapsulated into an efficient method, directly avoid multiple calls of methods.

(4) Debugging features in small projects do not need to use, with debugging-related code to remove all.

After I rewrite the template class at 4 points above, the execution time of a complex page is reduced by one order of magnitude (no other optimizations except the template).

Now optimize the interface between the abstraction layers of your program, especially if these interfaces are using ready-made functions or classes. Because these functions or classes are designed to sacrifice some efficiency in order to adapt to the general situation, their authors may not consider efficiency issues. The efficiency of such a well-known Phplib template class is not necessarily high.

2, Detail code optimization

(1) Template class optimization mentioned above, regular expression matching is much slower than normal string matching, and matches the string as much as possible without regular expression matching. Sometimes even though regular expression matching makes the program code more concise, and general string matching makes the code more omissions, many times string matching is still more efficient than regular expressions.

(2) The string substitution function Str_replace and Preg_replace both accept array parameters. Sometimes it is necessary to bulk replace a string, and an array parameter is more efficient than a circular invocation of a replacement function. For example, the following code:

for ($i = 0; $i < $n; $i + +) {
$str = Str_replace ($search [$i], $replace [$i], $STR);
}

Should be replaced by:

$str = Str_replace ($search, $replace,

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.