Optimize the PHP Program-PHP source code

Source: Internet
Author: User
Tags smarty template
Ec (2); & nbsp; many articles on the Internet introduce the optimization of php programs, which are implemented by installing acceleration software such as ZendOptimizer, but such acceleration is limited. This article mainly introduces some optimization methods from the program code. 1. The more abstract layers of a program, the stricter the separation of abstract layers, and the lower the program efficiency. The most primitive php program mode applied to web pages is the script embedding mode. That is, the php script is embedded by adding identifiers to the places where a Web page needs to dynamically process or display data. In general, script ec (2); script

Many articles on the Internet introduce how to optimize php programs by installing acceleration software such as Zend Optimizer, but such acceleration is limited. This article mainly introduces some optimization methods from the program code.

1. The more abstract layers of a program, the stricter the separation of abstract layers, and the lower the program efficiency.

The most primitive php program mode applied to web pages is the script embedding mode. That is, the php script is embedded by adding identifiers to the places where a Web page needs to dynamically process or display data. Generally, this is the earliest Learning Mode for php programmers. It has only one abstraction layer, namely the web page. Therefore, this article calls it a single-layer mode.

As the website grows, programmers may find it difficult to maintain programs in the single-layer mode. When you want to modify or expand the functions of a program, you may find that the code is very messy and you cannot start. As a result, the template class was born, which made a webpage composed of two files: a php program file and an html template file. Common Template classes include the Template class in the PHPLib Library and the Smarty Template class. With the addition of an additional processing program (Template Class), the program efficiency is reduced. If you do not believe it, test it yourself. In fact, in general, functions (the most primitive programming method) are more efficient than functions (process-oriented), and functions are more efficient than object encapsulation (Object-Oriented. Therefore, even in the compilation language, it is necessary to use C to write efficiently, instead of C ++, such as the kernel of the FreeBSD operating system. In the case of extreme efficiency, it is necessary to write in a sink.

In order to allow the program to adapt to multiple database systems or to easily convert the database system at any time, a class is often used to encapsulate the functions dealing with the database, in this way, you only need to replace the encapsulation class when converting the database system, and the main program does not need to be modified. Here, another class is used, and the efficiency is reduced.

The above template class divides the program into two abstract layers: The program layer and the presentation layer. The application of database classes divides the program layer into the data interface layer and data processing layer.

The larger the project, the more abstraction layers that need to be separated, which makes the division of labor clear and convenient for management, but at the cost of program execution efficiency.

To reduce the efficiency caused by the abstraction layer, there are two optimization methods: reduce the abstraction layer and optimize the interfaces between the abstraction layer. Generally, the abstraction layer should not be blindly reduced to improve efficiency, which will make the code messy and difficult to manage. However, you should not create too many abstraction layers for a small project unless you have plans to do a lot in the future. This article does not discuss how to properly split the abstraction layer in depth.

For the two layered examples mentioned above, the interfaces between the optimized abstraction layer are template classes and database operation classes. Abstract layer interfaces need to be frequently called in programs to exchange information between different layers. Therefore, layer interfaces are worthy of optimization. Database Interface classes may only encapsulate some database functions, but there is little room for optimization. There is a lot of room for optimization for template classes. Generally, the more common the Template model is, the more powerful the Template class is and the lower the efficiency is. For example, the Template class in the PHPLib library has great room for optimization. Compared with the PHPLib Template, the Smarty Template is more complex. I have never used it. It is said that there is a cache mechanism and I do not know whether it can make up for its performance loss. Next let's take a look at how many things of the PHPLib Template class can be optimized.

(1) When reading template files, the file function is inefficient. Instead, use the get_file_content function.

(2) When matching the sub-template, the regular expression replacement function preg_replace is inefficient. Instead, use the str_pos function to locate and use the str_replace function to replace the sub-template. This optimization method will be analyzed in detail later.

(3) The template model is highly versatile and can adapt to various situations. However, the efficiency of common methods may be very low in terms of handling specific details. You can modify the template model as needed. My approach is to create a relatively general template class, and then assign a template class that only applies to a specific program. Some optimizations can be made on the Template model of the general Template class (compared with the PHPLib Template). For example, it is complicated to process two-dimensional data tables with the PHPLib Template, the class method needs to be called multiple times (essentially a function call). Therefore, the function of processing two-dimensional data tables can be encapsulated into an efficient method to avoid multiple calls of the method.

(4) The debugging function is not required for small projects and all Code related to debugging is removed.

After I overwrite the template class at the above four points, the execution time of a complex page is reduced by an order of magnitude (no optimizations except the template ).

Optimize the interfaces between the abstract layers of your program, especially when these interfaces use ready-made functions or classes. Because these functions or classes are designed to adapt to the general situation at the expense of some efficiency, and their authors may not consider efficiency issues. The efficiency of such a famous PHPLib template class is not necessarily high.

2. Detailed code optimization

(1) As mentioned above in template optimization, regular expression matching is much slower than string matching. Try to use string matching instead of regular expression matching. Sometimes, although regular expression matching makes the program code more concise, and general string matching makes the code more redundant, string matching is still more efficient than regular expressions in many cases.

(2) string replacement functions str_replace and preg_replace can both accept array parameters. Sometimes you need to replace strings in batches, it is more efficient to use array parameters than to call the replacement function cyclically. For example, the following code:

For ($ I = 0; $ I <$ n; $ I ++ ){
$ Str = str_replace ($ search [$ I], $ replace [$ I], $ str );
}

Should be replaced:

$ 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.