Three tricks to make your PHP engine run at full speed_PHP Tutorial

WBOY
Release: 2016-07-15 13:25:53
Original
1055 people have browsed it

作为流行的 Web 编程语言, PHP 的最大优势就是速度。 PHP已经在这方面做的非常好了,你几乎找不到比它更快的脚本编程语言了。但是如果你的应用负荷很大,而带宽又比较小,或者有其他的瓶颈影响你的服务器性能,那么,你不妨试试笔者为你开出的几个药方,看看是否灵验。

一、代码优化

一谈到代码优化,或许你想到的就是整齐明了的代码,但是本文的意思却不是在此,因为如果要寻求速度的话,就要对PHP 源码作相应的调整。一般说来就是去掉多余的注释,让代码不可读。但是这对于一个具有良好素养的程序员来说,简直就是不可思议的。好在Zend Technologies 公司发布了Zend 优化引擎可以帮助你做到这一点。它现在是免费的,但是你必须遵循 Zend Optimizer 许可。这个产品可以对引擎产生的中间代码进行优化。

安装这个引擎比较简单,下载对应平台的版本以后,解开压缩文件,然后在 php.ini 文件里面加上下面两行,重新启动 Web 服务器,就搞定了。

zend_optimizer.optimization_level=15 zend_extension="/path/to/ZendOptimizer.so" zend_loader.enable=Off 
Copy after login

如果是 Win32 平台的应该是:

zend_optimizer.optimization_level=15 zend_extension_ts="C:\path\to\ZendOptimizer.dll" zend_loader.enable=Off 
Copy after login

Actually, the third line is optional. Since it seems like turning off zend_loader will improve the speed a bit, it's worth putting this third line into php.ini . It should be noted that the prerequisite for turning it off is that you are not using the Zend encryption program.

2. Buffering

If we want to further improve the speed, we need to consider using buffering technology. There are some alternative solutions, including Zend Cache (beta version), APC, and Afterburner Cache, as well as jpCache, etc.

The above are buffer modules. They store the intermediate code generated by the first request for the .php file in the memory of the web server, and then return the "compiled" version for subsequent requests. Because this reduces disk reads and writes, and all work in memory, this process can significantly improve application performance.

There are many such products readily available, so who should you choose?

◆Zend Cache is a good commercial product. After loading those large PHP pages for the first time, you will obviously feel the speed increase, and the server will set aside more resources. Unfortunately, this product costs money, but in some cases, you don't want to skimp on the money.

◆Afterburner Cache is a product of Bware Technologies. It is still in Beta version. It seems to be the same as Zend Cashe, but it cannot achieve as good results as Zend Cache, nor can it work with the Zend optimization engine, but It's free, so I adopted this module.

◆APC (Alternative PHP Cache) is another free module released by Community Connect, and it seems that it can be used in production environments.

3. Web Content Compression

For an increasingly congested network, saving bandwidth is as valuable as saving water. According to IETF standards, most browsers should support content compressed using gzip. This means you can send gzip-compressed content to the browser, and the browser will transparently decompress the data.

mod_gzip is a free Apache module launched by Remote Communications, which can compress static Web content and send it to the browser. For most static web pages, this module is suitable. Although the people at Remote Communications said that this module supports all the dynamic content generated by mod_php, mod_perl, mod and so on, it still doesn't seem to work. Judging from the mod_gzip mailing list, this problem is not expected to be solved until 1.3.14.6f.

If you want to compress dynamic content, we can use class.gzip_encode.php, a PHP class used at the beginning and end of the script. For the entire website, the functions in auto_prepend and auto_append of php.ini are called. For details, you can read the program of this class. This program is well commented and the author tells you almost everything. But before using it, your PHP must be compiled to support zlib.

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

output_handler = ob_gzhandler ;

This enables PHP to activate output buffering and compress all output. If there is any special reason why you don’t want all the content to be compressed and output, you can add the following line to the .htaccess file to compress the files in the corresponding directory.

php_value output_handler ob_gzhandler

You can also add it directly in the PHP code:

ob_start("ob_gzhandler");

This compression technology is very effective, but for Netscape Communicator users, because graphic files cannot be compressed, it seems that they are not sent completely, so the compression of jpeg and gif files must be turned off. IE does not have this problem.

4. Conclusion

Using the techniques discussed in this article should improve your website performance, but please note:

◆PHP possible It is not the cause of the bottleneck, carefully check other causes (for example: database)

◆You cannot adjust the server performance to the highest state. So before complaining about PHP and its buffering, consider whether it's time to upgrade your server or adopt dynamic load balancing technology.

◆ Don’t underestimate content compression. When you see a speed increase in PHP applications on your 100 Mb intranet, don’t forget where your modem users are complaining about your 100Kb HTML pages.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446646.htmlTechArticleAs a popular Web programming language, the biggest advantage of PHP is speed. PHP has done this very well, and you can hardly find a faster scripting language than it. But what if...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!