Home > Article > Backend Development > A complete guide to improving PHP execution speed (Part 1)_PHP Tutorial
One of the advantages of PHP is that it is very fast, which is sufficient for general website applications. However, if the site's traffic is high, bandwidth is narrow, or other factors cause performance bottlenecks on the server, you may have to think of other ways to further increase the speed of PHP. This article will introduce how to do this from several aspects, so as to make users browse more "cool".
Code Optimization
I don’t want to tell you again how to write cleaner code. I think everyone knows this. When you need speed, you may already be doing it. A lot of work has been done to optimize the PHP source code. What is proposed here is that this tedious work can be completed by other tools. This is Zend Optimizer, a program available for free from Zend Technologies' website (http://www.zend.com/). Its principle is simple, by detecting the intermediate code generated by the Zend engine and optimizing it to obtain higher execution speed. I think optimizing code is a rather 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, you may not know what to do yourself. Got it ;-). Therefore, I recommend that you use Zend Optimizer to do this optimization work when the PHP source code is relatively complex. The advantage is that it will not make your code complicated and difficult to understand.
Installing Zend Optimizer is very simple. Just download the relevant precompiled libraries according to the platform you are using, add two lines to your php.ini, and restart your web server!
zend_optimizer.optimization_level=15zend_extension="/path/to/ZendOptimizer.so" zend_loader.enable=Off
You may be a little strange, didn't it say two lines, why did it become three lines? . However, the third line is optional. 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 should be noted that zend_loader can only be disabled when you are not using Zend Encoder Runtime. Zend Encoder Runtime will be mentioned below.
Want it faster? Use cache
If your PHP application still needs faster speed, the next solution is caching. There are a few different ways to accomplish this. I have tried Zend Cache (evaluation version), APC and Afterburner Cache myself.
The above mentioned are all "buffer modules". Their principles are similar. When the PHP file is requested for the first time, by storing the intermediate code of your PHP source code in the memory of the web server, for the same subsequent requests, the "compiled" version in the memory is directly provided. Since it can minimize disk access, this method can indeed greatly improve the performance of PHP. What's more convenient is that 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 customers getting an old version of the program. These buffered modules are great, but which one should I choose? Let’s introduce them respectively:
Zend Cache is a commercial product of Zend Technologies (it is also the company that provides us with PHP engine and Zend Optimizer for free). It's really not bad. After the first run, you can obviously notice that the speed of PHP has been greatly improved, and the server has more free resources. The downside is that you have to pay for it, but in terms of value for money, it's still well worth it.
Afterburner Cache is a free buffer module provided by Bware Technologies (bwcache.bware.it). Currently only a beta version, it seems to do the same job as Zend Cache, but the performance improvement is not as good as Zend Cache, and the existing version does not work with Zend Optimizer, but it is free.
APC (Alternative PHP Cache) is another free module provided by Community Connect (apc.communityconnect.com). It works very stably and the speed has been greatly improved. It should be noted that I have not found an official test data. These are just tests on my application, so I cannot draw a conclusion.