PHP 回显性能

发布于 2024-10-24 22:35:26 字数 159 浏览 7 评论 0原文

这些命令中哪一个执行效果最佳?最糟糕?为什么?

echo 'A: '.$a.' B: '.$b.' C: '.$c;

echo 'A: ', $a, ' B: ', $b, ' C: ', $c;

echo "A: $a B: $b C: $c";

Which of these commands will perform the best? Worst? And why?

echo 'A: '.$a.' B: '.$b.' C: '.$c;

echo 'A: ', $a, ' B: ', $b, ' C: ', $c;

echo "A: $a B: $b C: $c";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空宴 2024-10-31 22:35:26
echo 'A: ', $a, ' B: ', $b, ' C: ', $c;

将是最快的,因为这里字符串的所有部分都直接复制到输出流,而其他变体将涉及首先连接字符串部分。 “串联”意味着必须为字符串的每个部分分配一个新的内存块,并将前一个字符串复制到其中。

我将用下面的例子来说明它

echo 'Hallo', ' ', 'World', '!', "\n", 'How are you?';
// vs.
echo 'Hallo' . ' ' . 'World' . '!' . "\n" . 'How are you?';

第一个复制5字节+ 1字节+ 5字节+ 1字节+ 1字节+ 12字节到输出流,从而复制25字节。
第二个创建一个 5 字节的字符串,然后创建一个 6 字节的字符串,并将 5 + 1 字节复制到其中,然后创建一个 11 字节的字符串,并将 6 + 5 字节复制到其中,然后创建一个 12 字节的字符串。 bytes 并将 11 + 1 字节复制到其中,然后创建一个 13 字节的字符串并将 12 + 1 字节复制到其中,然后创建一个 25 字节的字符串并将 13 + 12 字节复制到其中,最后复制这 25 个字节字节到输出缓冲区。这将复制 92 个字节并完成更多的内存分配。

但实际上,你不应该关心这个。我非常怀疑您的应用程序的瓶颈将是 echo 性能;)

但是我使用 echo 和逗号而不是连接运算符仍然有一个原因:逗号具有最低的所有运算符优先级。这样你就不必写括号了。

例如,这可以工作:

echo 'The script executed in ', microtime(true) - $startTime, ' seconds';

而这不会按预期工作(我认为这是未定义的行为):

echo 'The script executed in ' . microtime(true) - $startTime . ' seconds';
echo 'A: ', $a, ' B: ', $b, ' C: ', $c;

will be fastest, because here all parts of a string are directly copied to the output stream, whereas the other variants would involve first concatenation the string parts. "Concatenation" means that for every part of the string a new chunk of memory must be allocated and the previous string copied into it.

I will illustrate it with the following example

echo 'Hallo', ' ', 'World', '!', "\n", 'How are you?';
// vs.
echo 'Hallo' . ' ' . 'World' . '!' . "\n" . 'How are you?';

The first one copies 5 bytes + 1 byte + 5 bytes + 1 byte + 1 byte + 12 bytes to the output stream, thus copying 25 bytes.
The second one creates a string with 5 bytes, then creates a string with 6 bytes and copies the 5 + 1 bytes into it, then creates a string with 11 bytes and copies the 6 + 5 bytes into it, then creates a string with 12 bytes and copies the 11 + 1 bytes into it, then creates a string with 13 bytes and copies the 12 + 1 bytes into it, then creates a string with 25 bytes and copies the 13 + 12 bytes into it and then eventually copies these 25 bytes to the output buffer. That would be 92 bytes copied and way more memory allocations done.

But really, you shouldn't care about that. I very much doubt that the bottleneck of your application will be echo performance ;)

But there still is a reason why I use echo with commas instead of concatenation operators: The comma has the lowest of all operator precedences. That way you never have to write parenthesis.

For example this would work:

echo 'The script executed in ', microtime(true) - $startTime, ' seconds';

Whereas this would not work as expected (and is undefined behavior I think):

echo 'The script executed in ' . microtime(true) - $startTime . ' seconds';
七分※倦醒 2024-10-31 22:35:26

哦不,又是这个问题。

这些都不是最好的。

它们都是一样的。

此外,这些都不应该被使用。
它应该是输出数据的模板,并且您已经使用了它的功能,而不是直接从业务代码打印值。

再说一遍,没有速度差异。
任何超载的服务器都不会从这种“优化”中得到帮助。不是伟大的,甚至不是一丁点。如果你想帮助一个过载的服务器,你必须分析它的性能,而不是做无用的事情。

Oh no, this question again.

None of these perform the best.

They're all the same.

Moreover, none of these should be used ever.
It should be template, that outputs your data, and you have use it's features, not printing values from your business code directly.

And again, there is no speed difference.
No overloaded server will get no help from such "optimization". Not great one, nor even slightest. If you want to help an overloaded server, you have to profile its performance, not doing useless things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文