Tips to improve PHP running speed

Source: Internet
Author: User
Tags foreach apc ini php file php and php code php script zend

The biggest advantage of using PHP for


1, code optimization


Code optimization not only writes clean and clear code, but simplifies the code somewhat. 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 efficiency of the complex PHP source program is significantly improved, with the disadvantage that the optimized code is less readable and makes it difficult to modify the code. The


Zend Optimizer installation method is very simple, as long as the relevant precompiled version is downloaded according to the platform that the user is using, the following 2 lines of code are added to the php.ini file, and the Web server is restarted:


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 caching


If the size of a PHP program is large, 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. The


above 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 disk traffic (code has been read and parsed), and the code is shipped directly in memoryRows, making the server respond to requests much more quickly.


Of course, the cache module also monitors changes to the PHP source file and caches the page if necessary, preventing the user from getting a page that is 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 a commercial software developed by the 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 cache 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 free cache module developed by Community Connect, currently available in 2.0.4, from http:// PECL.PHP.NET/PACKAGE/APC obtained. For product applications, its performance is stable, and it can greatly increase the speed of response requests.

3, compress Web content


There are 1 important factors affecting the speed of a site's access, which 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. The


Mod_gzip is the 1 open source, Standard Apache module, also known as the Internet Content Accelerator 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. The


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


Output_handler = Ob_gzhandler


This activates the output buffering function of PHP and compresses before the content is sent. If you do not want to set up here, only in the need to change the default settings (not compressed), as long as the need to compress the PHP source directory, modify it.Htaccess the file, 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 tricks:


(1) Replace i=i+1 with I+=1, which is consistent with C + + 's habits, Efficiency is relatively higher. The


(2) uses PHP intrinsic functions whenever possible. When


(3) can use a single quote string, try to use a single quote string. Single quote strings are more efficient than double quote strings. The

(4) uses foreach instead of a while to traverse the array, and 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.