有沒有速度差,說:
$ newstring =「$ a和$ b出去看看$ c」;
and
$ newstring = $ a。 「和」。 $ b。 「出去看看」。 $ C;
如果是這樣,爲什麼?
有沒有速度差,說:
$ newstring =「$ a和$ b出去看看$ c」;
and
$ newstring = $ a。 「和」。 $ b。 「出去看看」。 $ C;
如果是這樣,爲什麼?
根據不同的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 />';
?>
是有,但不同的是
$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等)的雙引號內的內容,並且在使用單引號時,它不會分析變量或特殊字符,因此速度可能會略有增加。
有可能將是一個速度差,因爲這是兩個不同的語法。你需要問的是如果差異很重要。在這種情況下,不,我不認爲你需要擔心。這個差別太小了。
我會建議你做任何對你來說最有意義的視覺。 「$a and $b went out to see $c
」在查看時會有點混亂。如果你想走這條路,我會建議你的變量使用大括號:「{$a} and {$b} went out to see {$c}
」。
根據最少驚喜的原則,意味着完全相同的事物的兩種不同的語法應該具有相同的屬性。它可能不在PHP中的事實只是PHP的一個不好的屬性。 – 2009-11-28 21:01:17
@Ira:更糟糕......看到我的結果如下。在一個案例中最快的可能是另一個案例中最慢的。 – 2009-11-28 21:06:31
爲什麼你不測試它,並比較差異?數字不會撒謊,如果你發現一個比另一個更好,那麼你應該問爲什麼。
不知道爲什麼這個答案downvoted。這是使用PHP時最好的建議。 – 2009-11-28 21:24:38
因爲這不是一個有用的答案。我已經知道我可以自己嘗試。我把它發佈在這裏,看看其他人不得不說的。 – 2009-11-28 22:09:04
我做了一個快速基準測試,正如其他人所說的,結果非常不一致。我沒有注意到使用單引號而不是雙引號的性能增益。我的猜測是,這一切都歸結於偏好。
您可能想要堅持一種類型的報價,如果您符合您的編碼風格,請選擇雙引號。替換功能比您想象的更經常使用。
我把基準代碼on github。
如果您擔心此級別的字符串連接速度,那麼您使用的語言不正確。編譯C語言中的應用程序,並在PHP腳本中調用該應用程序,如果這個確實是是瓶頸。
什麼是非常有用的答案。這就像在波士頓告訴某人問天氣是否會變得溫暖,他們應該轉移到邁阿密。 – MikeSchinkel 2011-06-16 04:52:02
@MikeSchinkel:我想這個比喻應該是:「我想去魁北克露營,想知道:波士頓的天氣怎麼樣?」。答案很明顯:「魁北克不在波士頓,甚至不在美國,你正在尋找錯誤的答案。」說明哪種語法更快,將支持OP集中處理錯誤的信息,這無助於他成爲更好的開發人員。相同的想法(表達更積極)可以在這裏找到長度:http://weblogs.asp.net/alex_papadimoulis/archive/2005/05/25/408925.aspx – soulmerge 2011-06-16 13:20:52
@soulmerge - 鑑於你的看法使用工具可能是唯一正確的看法,但你必須每天處理那麼多不太聰明的人才會感到孤獨。 – MikeSchinkel 2011-06-17 09:48:26
沒有區別,期間。 ;)
當然有,但並不重要。從一個PHP版本到另一個PHP版本也不一致。 – 2009-11-28 23:21:01
從我所知道的字符串解析這是一個神話,單引號會更快。根據我的經驗,情況正好相反。 – 2009-11-28 21:02:20