As a walking lake for many years old Chinese medicine, today was ordered to solve a case of the front-end page show slow problem. The status of the problem page is as follows:
Apache + PHP
Using the Smarty template to output content
The final output of the page is large, 80k+
Page execution time is above 500ms
Sacrifice a magic weapon xhprof the problem page to do a careful check, found that the page bottleneck is actually a template (compiled) in an echo statement, the Echo statement output string is larger, probably 50k+ bytes, spend more than 400 milliseconds, accounting for the entire page execution time of 80%. Such echo output in the site home is actually very common things, there is no database operation, according to the reason should not be so long execution time.
So the search skills were used violently, and finally, in the Echo section of the PHP manual, some clues were found, as early as in 2003, when senior thought that the output of a large string through echo to the client will cause the server performance problem, as I tested, in this scenario, the use of print is also slow. The suggested solution is to cut the string into a smaller string output, and the display speed will be increased, and the output function is as follows:
<?php
function Echobig ($string, $bufferSize = 8192) {
$splitString = Str_split ($string, $bufferSize);
foreach ($splitString as $chunk)
Echo $chunk;
}
?>
But the above prescription is not very symptomatic, the entire ECHOBIG output time is still around 400 milliseconds, not much improvement.
Considering the output of a large number of content to the client is slow, so check the Apache configuration, the original has not been opened deflate compression, and then enabled. Again using xhprof for checking, this echo's output time is reduced to around 5ms.
400ms to 5ms, a configuration problem will produce 80 times times the gap, it is really save the old money. The story tells us that compressing the output is really important.
UPDATE: Speed up your echo http://www.php100.com/html/webkaifa/PHP/PHPyingyong/2011/1127/9361.html