Home >Backend Development >PHP Tutorial >A way to solve the slow loading of PHP_PHP tutorial

A way to solve the slow loading of PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:47:53970browse

In the past few days, when testing our current PHP framework, we found that the framework layer took a long time to load PHP files. Finally, we found that the entire loading time became longer due to various require_once. If we do not use eaccelerator, test it on a virtual machine. It may reach 50-60ms. After using EA, it can be reduced to 10-20ms. However, this consumption is still relatively large. Is there any way to solve it?

I happened to see an article on hiphop in the past two days, so I decided to use it to try it. In the end, I found that the cost was too high. To modify a file, you need to compile the entire project once, and the program it generates is http protocol. Now we just need A fastcgi server is enough, and the various extensions written by myself are completely unusable, so this approach is basically rejected.

Although hiphop is not going to work, it gave me a revelation. Since the slow loading is caused by require_once, then just write a script and merge all the current codes into one large file (because we The framework is RPC, so there is only one entry, index.php. The other codes are all classes and functions. There is no direct calling logic, so the merge will not have any impact in the future). The specific operations are as follows:

1. Delete all require_once lines and replace them with | blank lines

2. Replace the

3. Record the offset of each file in the merged file, and output this information at the end of the merged file. This is used to correct the log, because after the merge is completed, the information printed in the log is the merged one. The file and the number of lines are not helpful for offline log checking, so the log output class needs to be modified to correct the log based on the number of merged lines.

After finishing it, I actually measured it and found that if ea is not used, the consumption of the framework is basically 3-5ms. If ea is used, it is 5-6ms. I looked at the code of ea and found that it will parse The resulting op_array is stored in categories, and then each time zend_compile is called, these things are copied and returned. This process is not faster than zend_compile's actual compilation, and there are many additional checks, so it is better to use php-cgi directly.

In addition, I discovered a very strange phenomenon in PHP during testing

class A extends B

{

}

class B{}

This code is OK,

But

class A extend B{}

class B extend C{}

class C{}

Such code will go wrongwww.2cto.com

Note: Later I tested the autoload method and found that it is much slower than typing into a file. When only one or two classes are automatically loaded, a specific request takes about 20ms to execute, but typing into one only takes about 5ms

Excerpted from Wuxinyun

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478485.htmlTechArticleWhen testing our current php framework these days, we found that it takes a long time for the framework layer to load php files. Finally, It was found that various require_once causes the entire loading time to become longer. If you do not use it...
Statement:
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