2009-11-28 115 views
4

有没有速度差,说:

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

and

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

如果是这样,为什么?

回答

16

根据不同的PHP版本,它由第二多少是更快,如果你把它写喜欢变化: $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 

使用5.3这个代码与Zend Server CE的PHP:

<?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 />'; 

?> 
1

是有,但不同的是

$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等)的双引号内的内容,并且在使用单引号时,它不会分析变量或特殊字符,因此速度可能会略有增加。

+0

从我所知道的字符串解析这是一个神话,单引号会更快。根据我的经验,情况正好相反。 – 2009-11-28 21:02:20

6

有可能将是一个速度差,因为这是两个不同的语法。你需要问的是如果差异很重要。在这种情况下,不,我不认为你需要担心。这个差别太小了。

我会建议你做任何对你来说最有意义的视觉。 “$a and $b went out to see $c”在查看时会有点混乱。如果你想走这条路,我会建议你的变量使用大括号:“{$a} and {$b} went out to see {$c}”。

+0

根据最少惊喜的原则,意味着完全相同的事物的两种不同的语法应该具有相同的属性。它可能不在PHP中的事实只是PHP的一个不好的属性。 – 2009-11-28 21:01:17

+0

@Ira:更糟糕......看到我的结果如下。在一个案例中最快的可能是另一个案例中最慢的。 – 2009-11-28 21:06:31

1

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

+1

不知道为什么这个答案downvoted。这是使用PHP时最好的建议。 – 2009-11-28 21:24:38

+0

因为这不是一个有用的答案。我已经知道我可以自己尝试。我把它发布在这里,看看其他人不得不说的。 – 2009-11-28 22:09:04

2

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

您可能想要坚持一种类型的报价,如果您符合您的编码风格,请选择双引号。替换功能比您想象的更经常使用。

我把基准代码on github

2

如果您担心此级别的字符串连接速度,那么您使用的语言不正确。编译C语言中的应用程序,并在PHP脚本中调用该应用程序,如果这个确实是是瓶颈。

+0

什么是非常有用的答案。这就像在波士顿告诉某人问天气是否会变得温暖,他们应该转移到迈阿密。 – MikeSchinkel 2011-06-16 04:52:02

+0

@MikeSchinkel:我想这个比喻应该是:“我想去魁北克露营,想知道:波士顿的天气怎么样?”。答案很明显:“魁北克不在波士顿,甚至不在美国,你正在寻找错误的答案。”说明哪种语法更快,将支持OP集中处理错误的信息,这无助于他成为更好的开发人员。相同的想法(表达更积极)可以在这里找到长度:http://weblogs.asp.net/alex_papadimoulis/archive/2005/05/25/408925.aspx – soulmerge 2011-06-16 13:20:52

+0

@soulmerge - 鉴于你的看法使用工具可能是唯一正确的看法,但你必须每天处理那么多不太聪明的人才会感到孤独。 – MikeSchinkel 2011-06-17 09:48:26

-2

没有区别,期间。 ;)

+0

当然有,但并不重要。从一个PHP版本到另一个PHP版本也不一致。 – 2009-11-28 23:21:01

相关问题