Some tips to improve PHP running speed share

Source: Internet
Author: User
Tags foreach apc execution functions php file php and php code zend

One of the biggest advantages of using PHP is that it is fast. In general, PHP always has enough speed to support Web content dynamic generation, and many times it is impossible to find a faster method. However, when faced with huge traffic, high load applications, limited bandwidth, and other factors that create performance bottlenecks, you need to consider how to improve PHP performance

1, code optimization

Code optimization is not just writing clean and clear code, but simplifying the code a certain amount. You can use Zend Optimizer to help complete these miscellaneous tasks. Zend optimizer can be obtained from Zend Technologies's website http://www.zend.com/free of charge, but must agree to its license agreement, as it is not issued under the GPL. The principle is simple, that is, by detecting the intermediate code generated by the Zend engine and optimizing it to achieve higher execution speed.

After using the Zend Optimizer, the execution efficiency of the complex PHP source program will be greatly improved, the disadvantage is that the code is less readable after optimization, which makes it difficult to modify the code.

Zend Optimizer installation method is very simple, as long as the user using the platform to download the relevant precompiled version, the following 2 lines of code into the php.ini file, restart the Web server on the line:

Zend_optimizer.optimization_level=15

Zend_extension=″/path/to/zendoptimizer.so″

Zend_loader.enable=off

The extra third line of code is optional because disabling Zend_loader will make the optimization faster. It should be noted that Zend_loader can only be disabled if the Zend Encoder runtime is not used.

2, using the cache

If the PHP program is large, then the way to improve speed is to use caching. There are already a number of caching options available, including Zend Cache, APC, and Afterburner cache.

These are all "cache modules" (caching modules). The first time the PHP file is invoked, the cache module generates some intermediate code from the PHP source code and stores the intermediate code in the Web server's memory. When you call these files later, you can use the "compiled" code in memory directly. This approach does improve the performance of the application because it minimizes the amount of disk traffic (code has been read and parsed), and the code runs directly in memory, making the server respond to requests much more quickly.

Of course, the cache module also monitors the changes in the PHP source files, and when necessary, caches the pages, preventing users from getting pages that are still generated by outdated PHP code. Because caching modules can significantly reduce the load on the server and improve the response efficiency of PHP applications, they are ideal for Web sites with larger workloads.

Zend Cache is the commercial software developed by Zend Technologies Company. After the first run, the PHP page running speed will be greatly improved, the server has more free resources. The disadvantage is that it is not free, but the price is still very high.

Afterburner Cache is a free caching module developed by the Bware Technologies company. function and Zend Cache basically the same, but improve performance compared to Zend cache.

APC (alternative PHP cache) is another kind of free cache module developed by Community Connect Company, the current version is 2.0.4 and can be obtained from Http://pecl.php.net/package/APC. For product applications, its performance is stable, and it can greatly increase the speed of response requests.

3, compressed Web content

There are 1 important factors affecting the speed of the site's access, that is, download speed. The solution is to compress the content of the Web page. For plain text content, HTTP compression can be compressed to below 40% of its original size, providing more than 60% of data transfer savings. Although the Web server can cause a slight increase in CPU consumption because of compression, it saves a lot of network IO for transmission.

According to the IETF specification, most browsers support content compression using the gzip compression algorithm. That is, you can use gzip to compress the content of the Web page, and then send to the client browser, the browser will automatically extract data when receiving, and then display the page. This process is completely transparent to the user. Similarly, there are different ways to compress the contents of a Web page.

Mod_gzip is the 1 open source, Standard Apache module, also known as the Internet Content Acceleration module. It can be compiled with Apache, can also be used as DSO. Compared to the normal browsing process, it can save about 40% of the traffic. Mod_gzip not only can compress static content, such as HTML, XML, but also dynamically generated content, including SQL, Java, WML, VRML, and so on, real-time compression and transmission on the server side, its compression efficiency is astonishing, generally all for 60%~85%.

Compresses the contents of dynamic Web pages, and you can use Class.gzip to encode. php files, class.gzip to compress Web content by calling some of its functions at the beginning and end of a PHP script. If the entire site requires such compression, these functions can be called in the Auto_prepend and auto_append in the php.ini file, but will take up a certain overhead.

PHP4.0.4 introduced 1 new output buffering processing means-ob_gzhandler, its function and class.gzip exactly the same, the difference is that it can be directly added to the php.ini file, the syntax is as follows:

Output_handler = Ob_gzhandler;

This activates the output buffering function of PHP and compresses the content before it is sent. If you do not want to set up here, only in the need to change the default settings (not compression), as long as in the need to compress the PHP source directory, modify the. htaccess file is OK, the syntax is as follows:

Php_value Output_handler Ob_gzhandler

or call it directly in the PHP code:

Ob_start ("Ob_gzhandler");

The output buffering effect is ideal and does not incur additional overhead for the server. The point to note is that Netscape Communicator does not support image compression. Therefore, you must prohibit compression of JPEG and GIF images unless you know that your visitors are using Internet Explorer.

4 Other Tips

When you are programming, you can also speed up PHP by using a few tips:

(1) Replace i=i+1 with I+=1, which conforms to the habit of C + +, and has a higher efficiency.

(2) Use PHP internal functions whenever possible.

(3) When using single quote strings, use single quote strings as much as possible. Single quote strings are more efficient than double quote strings.

(4) by using foreach instead of the while traversal array, foreach is significantly more efficient than the while loop and does not need to call the Reset function.



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.