繁体   English   中英

PHP字符串连接 - “$ a $ b”vs $ a。 “”。 $ b - 表现

[英]PHP String concatenation - “$a $b” vs $a . “ ” . $b - performance

是否有速度差异,比如:

$ newstring =“$ a和$ b出去看$ c”;

$ newstring = $ a。 “和”。 $ b。 “出去看看”。 $ C;

如果是这样,为什么?

根据 PHP版本的不同,如果您将其编写为第二个更快,则会有所不同: $newstring = $a . ' and ' . $b . ' went out to see ' . $c; $newstring = $a . ' and ' . $b . ' went out to see ' . $c;

PHP在版本之间非常不一致 ,并且在性能方面进行构建,您必须自己测试它。 需要说的是,它还取决于$a$b$c ,如下所示。

当你使用" ,PHP解析字符串以查看其中是否有任何变量/占位符,但是如果你只使用' PHP将其视为一个简单的字符串而不进行任何进一步处理。所以一般来说'应该更快。至少在理论上。在实践中你必须测试。


结果(以秒为单位):

a, b, c are integers:
all inside "     : 1.2370789051056
split up using " : 1.2362520694733
split up using ' : 1.2344131469727

a, b, c are strings:
all inside "     : 0.67671513557434
split up using " : 0.7719099521637
split up using ' : 0.78600907325745  <--- this is always the slowest in the group. PHP, 'nough said

在Zend Server CE PHP 5.3中使用此代码:

<?php

echo 'a, b, c are integers:<br />';
$a = $b = $c = 123;

$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
    $newstring = "$a and $b went out to see $c";
$t = xdebug_time_index() - $t;
echo 'all inside " : ', $t, '<br />';

$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
    $newstring = $a . " and " . $b . " went out to see " . $c;
$t = xdebug_time_index() - $t;
echo 'split up using " : ', $t, '<br />';

$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
    $newstring = $a . ' and ' . $b . ' went out to see ' . $c;
$t = xdebug_time_index() - $t;
echo 'split up using \' : ', $t, '<br /><br />a, b, c are strings:<br />';

$a = $b = $c = '123';

$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
    $newstring = "$a and $b went out to see $c";
$t = xdebug_time_index() - $t;
echo 'all inside " : ', $t, '<br />';

$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
    $newstring = $a . " and " . $b . " went out to see " . $c;
$t = xdebug_time_index() - $t;
echo 'split up using " : ', $t, '<br />';

$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
    $newstring = $a . ' and ' . $b . ' went out to see ' . $c;
$t = xdebug_time_index() - $t;
echo 'split up using \' : ', $t, '<br />';

?>

可能存在速度差异,因为它有两种不同的语法。 你需要问的是差异是否重要。 在这种情况下,不,我认为你不必担心。 差异可以忽略不计。

我建议你做一些视觉上最有意义的事情。 $a and $b went out to see $c ”看起来有点令人困惑。 如果你想走这条路,我建议围绕你的变量大括号:“ {$a} and {$b} went out to see {$c} ”。

我做了一个快速的基准测试,正如其他人所说,结果非常不一致。 我没有注意到使用单引号而不是双引号的性能提升。 我的猜测是,这一切都取决于偏好。

您可能希望坚持使用一种类型的编码样式,如果这样做,请选择双引号。 替换功能比您想象的更频繁。

我把基准代码放在github上

如果您担心此级别的字符串连接速度,则使用的是错误的语言。 在C中为此用例编译应用程序,并在PHP脚本中调用它,如果这确实是一个瓶颈。

是的,但是之间的差异可以忽略不计

$newstring = "$a and $b went out to see $c";

$newstring = $a . " and " . $b . " went out to see " . $c;

如果您使用:

$newstring = $a . ' and ' . $b . ' went out to see ' . $c;

差异会稍微大一些(但可能仍然可以忽略不计),原因是,如果我没记错(我可能错了),PHP会扫描并解析变量和特殊字符的双引号内的内容(\\ t,\\ n等等)当使用单引号时,它不会解析变量或特殊字符,因此速度可能略有增加。

你为什么不测试它,并比较差异? 数字不是谎言,如果你发现一个表现比另一个好,那么你应该问为什么。

期间没有区别。 ;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM