Database Skill

Database Skill

About MySql, SQL Server, Oracle, PostgreSql, DB2, SyBase, SQL, T-SQL, PL-SQL.

The PHP echo why so slow

Tag: FUNCtion, string, the php, server Category: Database Author: aqmei Date: 2012-03-01

As a walking old Chinese rivers and lakes for many years, ordered to solve a case of the front page to show the problem of slow today. The problem page is as follows:

apache + php

Using smarty template output content

Page final output, 80k +

Page execution time in more than 500ms

To reposition itself magic weapon xhprof of on the page in question do a careful examination, the bottleneck of the page turned out to be the template (compiled) in an echo statement, the echo statement output string comparison, is about 50K + bytes, spend time 400 milliseconds, accounting for 80% of the execution time of the entire page. Echo output in the site home page is actually a very common thing, there is no database operations, stands to reason that the execution time should not be so long.

So violently use the search skills, and eventually find some clues in the php manual echo, as early as 2003 alone predecessors through the echo output string will cause the server to the client performance issues, as far as I test, in this scenario use the print is actually the same slow. The proposed solution is the string into smaller strings output, show the speed will improve output functions are as follows:

??<?php
??function echobig($string, $bufferSize = 8192) {
??$splitString = str_split($string, $bufferSize);
??foreach($splitString as $chunk)
??echo $chunk;
??}
???>

But at the prescription is not symptomatic, entire echobig output time still in about 400 milliseconds, there is not much improvement.

Taking into account the output of a large amount of content to the client slower, so check the apache configuration, the original not open the deflate compression, then enabled. The use again xhprof check that the output of echo time is reduced to about 5ms.

400ms to 5ms, a configuration will have 80 times the gap really is the province of the old money. This story tells us, the compressed output is really important.

UPDATE: speed up your echo