Why is the echo of PHP so slow

Source: Internet
Author: User

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







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.