How to Improve the php Running Speed ?, Increase php running speed?

Source: Internet
Author: User
Tags apc

How to Improve the php Running Speed ?, Increase php running speed?

One of the biggest advantages of using PHP is its high speed. In general, PHP always has enough speed to support dynamic generation of Web content, and many times it cannot even find a faster method than it. However, when faced with huge traffic volumes, high-load applications, limited bandwidth, and other factors that bring performance bottlenecks, you need to consider how to improve the performance of PHP.
1. code optimization
Code optimization not only writes clean and clear code, but also simplifies the code. Zend Optimizer can be used to automatically complete these complex tasks. Zend Optimizer can be obtained from Zend Technologies's website http://www.zend.com/for free, but the author agrees with the license, because it is not published in gpl format. Its principle is very simple, that is, by detecting the intermediate code generated by the Zend engine and optimizing it to achieve higher execution speed.
After Zend Optimizer is used, the execution efficiency of complex PHP source programs will be significantly improved. The disadvantage is that the code readability after optimization is reduced, which makes it difficult to modify the code.
The Zend Optimizer installation method is very simple. You only need to download the pre-compiled version based on your platform and add the following two lines of code to php. in the INI file, restart the Web Server:
Zend_optimizer.optimization_level = 15
Zend_extension = "/path/to/ZendOptimizer. so ″
Zend_loader.enable = Off
The third line of code is optional because disabling zend_loader will make optimization faster. Zend_loader can be disabled only when Zend Encoder Runtime is not used.
2. Use Cache
If the PHP program is large, the way to increase the speed is to use the cache. There are already many caching schemes available, including Zend Cache, APC, and Afterburner Cache.
The above are all cache modules ). When the PHP file is called for the first time, the cache module generates some intermediate code from the PHP source code and stores the intermediate code in the memory of the Web server. When you call these files later, you can directly use the code compiled in the memory. This method can indeed improve the performance of the application because it minimizes the disk access volume (the code has been read and parsed) and the code runs directly in the memory, this greatly improves the server's response speed.
Of course, the cache module also monitors changes in PHP source files and caches pages again when necessary to prevent users from generating pages from outdated PHP code. Because the cache module can significantly reduce the server load and improve the response efficiency of PHP applications, it is very suitable for websites with large loads.
Zend Cache is a commercial software developed by Zend Technologies. After the first running, the running speed of the PHP page will be greatly improved immediately, and there will be more idle resources on the server. The disadvantage is that it is not free, but it is very cost-effective.
Afterburner Cache is a free Cache module developed by Bware Technologies. The function is basically the same as that of Zend Cache, but the performance improvement is inferior to that of Zend Cache.
APC (Alternative PHP Cache) is another free Cache module developed by Community Connect. The current version is 2.0.4 and can be found at http://pecl.php.net/package/apc. For product applications, the performance is stable and the request response speed can be greatly improved.
3. Compress webpage content
There is also an important factor affecting the Website access speed, that is, the download speed. The solution is to compress the webpage content. For plain text content, HTTP compression can be compressed to less than 40% of the original size, thus saving data transmission by more than 60%. Although the Web server will slightly increase CPU usage due to compression, it can save a lot of network IO for transmission.
According to the IETF specification, most browsers support using gzip compression algorithms for content compression. In other words, you can use gzip to compress the webpage content and then send it to the client browser. The browser automatically decompress the data and then displays the page. This process is completely transparent to users. Similarly, there are different ways to compress web pages.
Mod_gzip is an open-source and standard Apache module, also called the Internet Content Acceleration module. It can be compiled with Apache or used as a DSO. Compared with the common browsing process, it can save about 40% of the traffic. Mod_gzip can not only compress static content, such as HTML and XML, but also dynamically generated content, including SQL, Java, WML, and fig, real-time compression and transmission on the server side, the compression efficiency is amazing, generally 60% ~ 85%.
You can also use class.gzipto compress the content of a dynamic web page by calling some of its functions at the beginning and end of the PHP script. If the whole site requires such compression, you can call these functions in the auto_prepend and auto_append files of php. ini, but it will occupy a certain amount of system overhead.
Php4.0.4pushes a new output buffer processing script-ob_gzhandler. the handler class is exactly the same as class.gzip. The difference is that it can be directly added to the php. ini file. The syntax is as follows:
Output_handler = ob_gzhandler;
This will activate the PHP output buffer function and compress the content before sending it. If you do not want to set it here, you only need to change the default setting (not compressed) as needed. You only need to modify it in the PHP source program directory to be compressed. the syntax is as follows:
Php_value output_handler ob_gzhandler
Or call it directly in PHP code:
Ob_start ("ob_gzhandler ");
The effect of the output buffer is indeed ideal, and it does not bring additional system overhead to the server. Note that Netscape Communicator does not support image compression. Therefore, you must disable jpeg and gif images unless you know that all visitors use Internet Explorer.
4 other skills
When programming, using some tips can speed up the PHP operation:
(1) Replace I = I + 1 with I + = 1, which conforms to the habit of c/c ++ and is more efficient.
(2) Use PHP internal functions as much as possible.
(3) When single quotes can be used, try to use single quotes. The efficiency of a single quotation mark string is higher than that of a double quotation mark string.
(4) use foreach instead of while to traverse the array. foreach is much more efficient than while loop and does not need to call the reset function.

Related Article

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.