Three tricks to get your PHP engine running at full speed _php tutorial

Source: Internet
Author: User
Tags apc php source code
As a popular Web programming language, PHP's biggest advantage is speed. PHP4 has done very well in this area and you can hardly find a faster scripting language than it does. But if your application is heavy and bandwidth is small, or if there are other bottlenecks affecting your server's performance, then you might as well try out some of the prescriptions that I've prescribed for you to see if it worked.

First, the Code optimization

When it comes to code optimization, perhaps you think of a neat and clear code, but the meaning of this article is not here, because if you want to seek speed, the PHP source code should be adjusted accordingly. In general, the extra annotations are removed, making the code unreadable. But for a good-quality programmer, it's incredible. Fortunately Zend Technologies Company released the Zend optimization engine to help you do this. It is now free, but you must follow Zend Optimizer license. This product can optimize the intermediate code generated by the engine.

Install this engine is relatively simple, download the corresponding version of the platform, unpack the compressed file, and then add the following two lines in the php.ini file, restart the Web server, it is done.

Zend_optimizer.optimization_level=15

zend_extension= "/path/to/zendoptimizer.so"

Zend_loader.enable=off

If it is the Win32 platform, it should be:

Zend_optimizer.optimization_level=15

Zend_extension_ts= "C:\path\to\ZendOptimizer.dll"

Zend_loader.enable=off

Ah! You're not mistaken, are you? What's the three line? In fact, the third line is optional. It's worth putting this third line into php.ini because it looks like turning the zend_loader off can improve a bit of speed. It is important to note that you are not using the Zend encryption program when you turn off the condition.

Second, buffer

If we want to increase our speed further, we need to consider using buffering technology. There are some optional solutions, including Zend cache (Beta version), APC, and Afterburner cache, plus jpcache.

All of these are buffer modules, which they put in the memory of the WEB server for the first time a request for a. php file was made, and then return a "compiled" version for subsequent requests. Because it reduces disk reads and writes and works in memory, this process can significantly improve application performance,

Ready-made products of this kind are more, in the end choose who?

Zend Cache is a good commercial product, after the first load of those large PHP pages, you will obviously feel the speed of ascension, the server will set aside more resources. It's a pity that this product is going to cost money, but in some cases you don't want to be stingy with the silver.

Afterburner Cache is a bware Technologies product that is currently in Beta and looks like Zend Cashe, but it does not work as well as Zend Cache, nor with Zend optimization engine , but it's free, so I used this module.

APC (Alternative PHP Cache) is another free module released by Community Connect and looks like it can be used in production environments.

Third, Web content compression

For an increasingly congested network, saving bandwidth is just as important as saving water. Depending on the IETF standard, most browsers should support content that uses gzip compression. In other words, you can send gzip compressed content to the browser, the browser will transparently unzip the data.

Mod_gzip is a free Apache module launched by Remote Communications, which compresses and sends static Web content to the browser. This module is suitable for most static web pages. Although

Remotecommunications's people say that this module supports all those mod_php, mod_perl,mod what produces dynamic content, but still doesn't seem to work, judging from the Mod_gzip mailing list, This problem is estimated to be solved by 1.3.14.6f.

If we want to compress dynamic content, we can use class.gzip_encode.php, a PHP class that is used at the beginning and end of the script. For the entire Web site is the function that is called in PHP.ini's Auto_prepend and Auto_append. You can read this kind of program in detail, the program is very well commented, the author almost told you everything. But before using it, your PHP will be compiled to support zlib.

For PHP 4.0.4, a new solution is to use the Ob_gzhandler, can achieve the same as the above class effect, as long as the simple in php.ini add the following sentence can be:

Output_handler = Ob_gzhandler;

This enables PHP to activate the output buffer and compress all output. If there is any special reason not to let all the content compressed output, you can use the following line in the. htaccess file to compress the files in the corresponding directory.

Php_value Output_handler Ob_gzhandler

You can also add directly to the PHP code:

Ob_start ("Ob_gzhandler");

This compression technique is very effective, but for Netscape Communicator users, because the graphics file cannot be compressed, it does not appear to be a complete send, so you must turn off compression of JPEG and GIF files, IE does not have this problem.

Conclusion:

Using the techniques discussed in this article should improve the performance of your website, but it is important to note that:

-PHP may not be the cause of the bottleneck, check for other reasons (for example: database)

-You can't tune server performance to the highest state. So before complaining about PHP and its buffers, consider whether you should upgrade the server or use dynamic load balancing (that's a lot of silver).

-Don't underestimate content compression, and when you see the speed boost for your PHP app on your internal network, don't forget where your modem users are complaining about your 100Kb HTML page.

http://www.bkjia.com/PHPjc/508473.html www.bkjia.com true http://www.bkjia.com/PHPjc/508473.html techarticle as a popular Web programming language, PHP's biggest advantage is speed. PHP4 has done very well in this area and you can hardly find a faster scripting language than it does. But if ...

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