PHP is slow when printing large amount of text
I made an ajax application, which worked great on my local dev server, but when I moved it online, one particular request got really slow. This request is quite complicated - it loads a lot of stuff from database and creates quite big text output, around 120kB. Since the app was written in a short time, there was a lot of space for optimization. Naturally I was trying to find what's slowing my app most - and I was surprised - it was the last final echo which was printing all calculated information.
I used Firebug to measure the times. The request took ~100ms without printing the info, just calculating, but ~400ms with printing... so the simple echo command took around 300ms! Then I tried PHP's microtime() to get more precise results... but suddenly there was no significant difference between printing and not printing. So I guess the problem is somewhere else - in the area of sending text开发者_运维百科 to Apache and then to the client... I don't understand this stuff, but I read somewhere that this might be caused by small Apache buffer. Can I do something about it? I don't think that 120kB is too much - just few years ago, in the time of table layouts, most of the big websites had html source of this size.
Could this be a problem of the webhosting? I could try to contact them but naturally it would be easier if I could solve this myself.
The answer is really simple. My download speed is around 300kB/s so downloading a 120kB page can't be faster than ~300ms. That's all and I'm really stupid :D
ob_start();
echo $huge_string;
ob_end_flush();
精彩评论