我知道以下三个声明会产生相同的输出:
echo "Hello" . "World! <br/>";
echo "Hello"; echo "World!", "<br/>";
echo "Hello", "World!", "<br/>";
但是,性能差异是什么?为什么?
"String" . "String"
与"String","String"
相比,连接速度较慢吗?
答案 0 :(得分:5)
这取决于你的表现你的意思......就操作次数而言,你的第一个例子是最好的(操作次数最少)......你的第二个和第三个例子几乎完全一样。
echo "Hello" . "World! <br/>";
HERE
Finding entry points
Branch analysis from position: 0
Return found
filename: /in/oYvSm
function name: (null)
number of ops: 3
compiled vars: none
line # * op fetch ext return operands
---------------------------------------------------------------------------------
3 0 > CONCAT ~0 'Hello', 'World%21+%3Cbr%2F%3E'
1 ECHO ~0
5 2 > RETURN 1
branch: # 0; line: 3- 5; sop: 0; eop: 2
path #1: 0,
echo "Hello"; echo "World!", "<br/>";
HERE
Finding entry points
Branch analysis from position: 0
Return found
filename: /in/nMufh
function name: (null)
number of ops: 4
compiled vars: none
line # * op fetch ext return operands
---------------------------------------------------------------------------------
3 0 > ECHO 'Hello'
1 ECHO 'World%21'
2 ECHO '%3Cbr%2F%3E'
5 3 > RETURN 1
branch: # 0; line: 3- 5; sop: 0; eop: 3
path #1: 0,
echo "Hello", "World!", "<br/>";
HERE
Finding entry points
Branch analysis from position: 0
Return found
filename: /in/LnPaY
function name: (null)
number of ops: 4
compiled vars: none
line # * op fetch ext return operands
---------------------------------------------------------------------------------
3 0 > ECHO 'Hello'
1 ECHO 'World%21'
2 ECHO '%3Cbr%2F%3E'
4 3 > RETURN 1
branch: # 0; line: 3- 4; sop: 0; eop: 3
path #1: 0,
因此,您可以清楚地看到您在第二个和第三个示例中使用了一个额外的echo
操作。但是,在这些示例中,性能(读取速度)几乎可以忽略不计。
答案 1 :(得分:2)
点运算符是最快的。您可以通过将其放入循环并对输出进行计时来测试它,如下所示:
http://www.sitepoint.com/high-performance-string-concatenation-in-php/
答案 2 :(得分:1)
第2行和第3行是等效的。
使用逗号会稍微快一些,因为它没有使用连接。