【发布时间】:2012-12-18 10:08:17
【问题描述】:
我有一个大约 6350268 个字符的字符串,当我将它们输出到浏览器时,它至少需要 700 毫秒,但通常需要 900 毫秒到 1000 毫秒的额外执行时间来输出它。
一代只占用300ms左右,但加起来几乎是一秒。
有没有办法加快回声输出?还是受输出到的浏览器限制?
// What this code does is take about 20.000 urls in the database and
// and transform it into a collapsable folder tree
// this process generates about 6300000 characters in about 300ms.
$this->benchmark->mark('code_start');
$query = $this->db->query("SELECT `url` from `site_pages` WHERE `url` not like '' order by `url` ASC");
$arr = array();
foreach($query->result() as $result)
{
$arr[$result->url] = $result->url;
}
$tree = $this->explodeTree($arr,'/',true);
$str = $this->plotTree($tree);
echo $str;
$this->benchmark->mark('code_end');
echo $this->benchmark->elapsed_time('code_start', 'code_end');
echo "<P>done";
【问题讨论】:
-
注释掉回显后性能会怎样?当您打开输出缓冲时怎么样?您在这里谈论的是 6 兆的数据, 将涉及一些处理时间......
-
您正在向浏览器输出 6MB?无论你怎么看,这个过程的一切都注定是缓慢的......
-
如果我注释掉回声,生成内容只需要 300-360 毫秒。我必须注意,我的服务器使用 gzip 压缩,因此实际输出要小得多。
-
我的猜测是 PHP 缓冲区和 Web 服务器缓冲/发送内容的组合会花费这么多时间。担心这个特殊的瓶颈似乎是错误的关注点。您不应在浏览器上转储 6MB 的数据,而应使用更温和的 AJAX 加载。
-
杀掉gzip压缩再试一次。
标签: php performance echo