PHP速度-许多回声与构建字符串 [英] PHP Speed - Many Echos vs Building a String

查看:52
本文介绍了PHP速度-许多回声与构建字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道是否有人知道以下两种方法中的一种会更快地产生输出:

Wondering if anyone knows if either of these methods would produce an output faster:

Method 1
for ($i=1;$i<99999;$i++) {
echo $i, '<br>';
}

Method 2
for ($i=1;$i<99999;$i++) {
$string .= $i . '<br>';
}
echo $string;

感谢您的任何输入。

推荐答案

方法1似乎会更快。方法2必须为每次循环迭代吐出一堆 CONCAT 操作码,并且很长的字符串将内置在内存中,直到您准备好发送它为止。另一方面,方法1每个循环只有两个 ECHO 操作码,然后PHP /您的网络服务器可以在完全完成操作之前自由地将内容刷新到客户端,如果

Method 1 seems like it'd be faster. Method 2 will have to spit out a bunch of CONCAT opcodes for each iteration of the loop, and the very long string will be built in memory until you're ready to send it. Method 1 on the other hand will just be two ECHO opcodes per loop, and then PHP/your webserver is free to flush content to the client before you've fully finished, if it wants to.

当然,如果您担心微优化,则可以通过使用操作码缓存,缓存代理,或类似 hiphop

Of course, if you're concerned about micro-optimisation, you're going to get far better performance by using an opcode cache, caching proxy, or something like hiphop.

这篇关于PHP速度-许多回声与构建字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆