Three tricks to make the PHP engine run fast

Source: Internet
Author: User
Tags apc website performance

PHPThe unique syntax is a mix of C, Java, Perl, and PHP self-innovative syntaxes. It can execute dynamic web pages more quickly than CGI or Perl. Compared with other programming languages, PHP embeds programs in HTML documents for execution on dynamic pages made using PHP, which is much more efficient than generating CGI with HTML tags completely. The following describes three methods to make the PHP engine run quickly.

I. code optimization

When it comes to code optimization, you may think of neat and clear code, but this article does not mean it here, because if you want to seek speed, you need to adjust the PHP Source Code accordingly. In general, it is to remove unnecessary comments and make the code unreadable. However, this is incredible for a good programmer. Fortunately, ZendTechnologies has released the Zend Optimization engine to help you achieve this. It is free now, but you must follow the Zend Optimizer license. This product can optimize the intermediate code generated by the engine.

It is relatively simple to install this engine. After downloading the version of the corresponding platform, uncompress the compressed file and add the following two lines to the php. ini file to restart the Web server.

 
 
  1. zend_optimizer.optimization_level=15  
  2. zend_extension="/path/to/ZendOptimizer.so" 
  3. zend_loader.enable=Off 

For the Win32 platform:

 
 
  1. zend_optimizer.optimization_level=15  
  2. zend_extension_ts="C:\path\to\ZendOptimizer.dll" 
  3. zend_loader.enable=Off 

In fact, the third row is optional. Because it seems that turning zend_loader off can improve the speed, it is worth putting the third line in php. ini. Note that you are not using Zend encryption.

Ii. Buffering

If we want to further speed up, we need to consider using the buffer technology. There are some optional solutions, including Zend Cache (test version), APC, Afterburner Cache, and jpCache.

These are all buffer modules. they store the intermediate code generated for the first request to the. php file in the memory of the Web server, and then return the compiled version for future requests. Because this reduces disk read/write and all work in the memory, this process can significantly improve application performance. There are many ready-made products. Who should I choose?

  • Zend Cache is a good commercial product. After loading those huge PHP pages for the first time, you will obviously feel the speed improvement and the server will set aside more resources. It is a pity that this product is for silver, but in some cases, you should not be stingy with the silver.
  • Afterburner Cache is a product of Bware Technologies and is currently in Beta version. It seems to be the same as Zend Cashe, but it cannot achieve the same effect as Zend Cache and cannot work with Zend Optimization engine, but it is free, so I use this module.
  • APC (Alternative PHP Cache) is another free module released by Community Connect. It seems that it can be used in the production environment.

Iii. Web content Compression

For an increasingly crowded network, saving bandwidth is just like saving water. According to IETF standards, most browsers should support gzip compressed content. In other words, you can send the content compressed with gzip to the browser, and the browser will transparently decompress the data.

Mod_gzip is a free Apache module released by Remote Communications. It compresses static Web content and sends it to browsers. This module is suitable for most static Web pages. Although Remote Communications said this module supports all the dynamic content generated by mod_php, mod_perl, and mod, it still does not seem to work. From the mod_gzip mailing list, it is estimated that this problem can be solved only by 1.3.14.6f.

If you want to compress the dynamic content, you can use class.gzip _ encode. php, a PHP class used at the beginning and end of the script. For the entire website, the function is called in the auto_prepend and auto_append of php. ini. For details, you can read the program of this class. This program is well annotated and the author tells you almost everything. However, before using it, Your PHP should be compiled to support zlib.

For PHP 5, a new solution is to use ob_gzhandler to achieve the same effect as the above class. Simply Add the following sentence to php. ini:

 
 
  1. output_handler = ob_gzhandler ; 

This allows PHP to activate the output buffer and compress all output data. If you do not want to compress the output of all the content for any special reason, you can add the following lines to the. htaccess file to compress the files in the corresponding directory.

Php_value output_handler ob_gzhandler

You can also add the following directly in the PHP code:

 
 
  1. ob_start("ob_gzhandler"); 

This compression technology is very effective, but for Netscape Communicator users, because they cannot compress graphical files, it does not seem to be completely sent. Therefore, they must disable the compression of jpeg and gif files, IE does not have this problem.

Iv. Conclusion

Using the technology discussed in this article should improve your website performance, but note the following:

PHP may not be the cause of the bottleneck. Check other causes carefully (for example, database)

You cannot adjust the server performance to the highest level. Therefore, before complaining about php and its buffer, consider whether to upgrade the server or use dynamic load balancing technology.

Do not underestimate the content compression. When you see the PHP application speed improvement on your 100 Mb Intranet, do not forget where the modem user complained about your kb HTML page.

We hope that the above content will help you.


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.