Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to improve the running speed of PHP

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to improve the running speed of PHP". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Code optimization

I don't want to tell you how to write cleaner code again. I think everyone knows that you may have done a lot of work on optimizing PHP source code when speed is needed. what is suggested here is that this tedious work can be done by other tools. This is ZendOptimizer, and this program is available for free from ZendTechnologies's website (http://www.zend.com/)). Its principle is very simple, by detecting the intermediate code generated by the Zend engine and optimizing it to achieve higher execution speed. I think optimizing the code is a tedious task, and the optimized code may become difficult to understand, especially when you put down the PHP program for a while and suddenly the customer asks you to make some changes. So I suggest you use ZendOptimizer to do this optimization when the PHP source code is complex. The advantage is that it won't make your code complicated.

Installing ZendOptimizer is very simple. Just download the relevant precompiled library according to the platform you use, add two lines to your php.ini, and restart your web server!

Zend_optimizer.optimization_level=15

Zend_extension= "/ path/to/ZendOptimizer.so"

Zend_loader.enable=Off

You may be a little surprised, didn't you say two lines, how to become three lines. However, the third line is optional, and it seems that disabling this zend_loader will make the optimization faster, so you might as well add this line to your php.ini file. It is important to note that you can disable zend_loader only if you are not using ZendEncoderRuntime.

If your PHP app needs to be faster, the next option is buffering. There are several different ways to achieve this. I have tried ZendCache (beta version), APC and AfterburnerCache myself.

All the above mentioned are "buffer modules". The principle is similar: when the php file is first requested, by storing the intermediate code of your PHP source code in the memory of the web server, a "compiled" version of the same request is provided directly in memory. Because it minimizes disk access, this approach does greatly improve the performance of PHP. More conveniently, when your PHP source code is modified, the buffered module can detect these changes and reload the same, so you don't have to worry about the customer getting the old version of the program. These buffering modules are good, but which one should I choose? The following are introduced separately:

ZendCache is a commercial product of ZendTechnologies (it is also the company that provides us with the PHP engine and ZendOptimizer for free). It's really good. After running it for the first time, you can clearly notice that the speed of PHP has been greatly improved and the server has more free resources. The disadvantage is that you have to pay for it, but in terms of performance-to-price ratio, it is still very worthwhile.

AfterburnerCache is a free buffer module provided by BwareTechnologies (http://bwcache.bware.it/)). Currently only the beta version, it seems to do the same work as ZendCache, but the performance improvement is not as good as ZendCache, and the existing version does not work with ZendOptimizer, but it is free.

APC (AlternativePHPCache) is another free module provided by CommunityConnect (http://apc.communityconnect.com/)). Its work is very stable, and the speed has improved a lot. It is important to note that I have not found an official test data. These are just tests on my application, so I cannot draw a conclusion.

Compression of Web content (makes it easier for your customers to use)

After the above two methods, I believe that the performance of your PHP application has been greatly improved, and now it is time to consider another aspect: download speed. If your application is just running within the company, and all customers connect to the server using 100Mb/s 's Ethernet, this may not be a problem, but if your customers use slow modem connections, you should consider using content compression. According to the IETF specification, most browsers support gzip content compression. This means that you can use gzip to compress the web content before sending it to the customer's browser, and the browser will automatically extract the data when it is received and let the user see the original page. Similarly, there are several different ways to compress the content of a web page.

Mod_gzip is a free Apache module provided by RemoteCommunications (http://www.phpbuilder.com/columns/www.remotecommunications.com)), which can compress static web pages. It works so well that you just need to compile it with apache (or use it as a DSO). People at Remotecommunications say it can also compress dynamic content, including mod_php,mod_perl. But I gave it a try, and it didn't work. I learned from mod_gzip 's mailing list that this bug will be fixed in the next version (1.3.14.6f, I think). However, you can still use it for static content compression.

But we also want to compress dynamic content, so we have to find another way. One way is to use class.gzipencode.php (http://leknor.com/code/), which can compress your page content by calling the PHP class at the beginning and end of your PHP script. If the entire site needs this compression, you can call these functions in auto_prepend and auto_append in your php.ini file. It works well, but it obviously brings a little bit of overhead on heavily loaded sites. To learn more about how it works, take a look at its class code (at least you need to add zlib support when compiling PHP). The author's instructions in it are also very detailed, and you can get anything you need to know.

Recently, I also read an article about PHP output buffering. What it says is that PHP4.0.4 has introduced a new way to handle output buffering-- ob_gzhandler, which works in the same way as the class described above, but the difference is that you only need to use the following syntax in your php.ini:

Output_handler=ob_gzhandler

This will activate PHP's output buffering function and compress everything it sends. For some special reasons, if you don't want to set it here, change this default setting only where you need it (do not compress it). Just modify the .htaccess file in the PHP source directory that needs to be compressed, using the following syntax:

Php_valueoutput_handlerob_gzhandler

... Or call it directly in your PHP code, in the following way:

Ob_start ("ob_gzhandler")

This method of output buffering is good and does not incur additional overhead for the server. I highly recommend that you use this method. Its change can be illustrated by the following example, if the customer is using a 28.8K modem, after this processing, he will think that it is suddenly replaced with an ISDN access. It is important to note that NetscapeCommunicator does not support image compression, so it will not be displayed. So unless all your customers use InternetExplorer, you must disable compression of jpeg and gif images. There should be no problem with the compression of other files, but I suggest you test it, especially if the browser uses an unusual plug-in or a less popular browser.

Other useful things.

ZendTechnologies's online store opened on January 24 this year and sells some interesting products related to PHP. Include the aforementioned ZendCache,ZendEncoder (simply put, a compiler of PHP code that produces compiled classes so that you can sell them to customers without worrying about leaking the source code. On web servers that need to run these classes, you will use ZendEncoderRuntime to decode), ZendIde (an integrated development environment for PHP with a lot of powerful performance), and support services for PHP developers.

Conclusion

What aspects should be paid attention to to improve the speed of PHP

Using the techniques mentioned in this article, you can greatly improve the performance of your site, but please note the following:

1. The bottleneck may not be PHP, you need to look at every object in the application (such as a database)

two。 The performance of a web server is limited, so don't think that poor performance is the reason for PHP, or it may be because of heavy traffic, your server needs to be upgraded, or consider using a load balancing system (which will cost a lot of money)

3. Don't think that content compression is not important. Your PHP application may perform well on 100Mb/s 's LAN, but consider users who use slow modem.

This is the end of "how to improve the running speed of PHP". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report