我知道以下三个语句将产生相同的输出:
echo "Hello" . "World! <br/>";
echo "Hello"; echo "World!", "<br/>";
echo "Hello", "World!", "<br/>";
但是性能差异是什么,为什么?
"String" . "String"
与 相比,连接速度慢"String","String"
吗?
我知道以下三个语句将产生相同的输出:
echo "Hello" . "World! <br/>";
echo "Hello"; echo "World!", "<br/>";
echo "Hello", "World!", "<br/>";
但是性能差异是什么,为什么?
"String" . "String"
与 相比,连接速度慢"String","String"
吗?
这取决于您所说的性能...就操作数量而言,您的第一个示例是最好的(操作数量最少)...您的第二个和第三个示例几乎完全相同。
HERE的操作码echo "Hello" . "World! <br/>";
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,
HERE的操作码echo "Hello"; echo "World!", "<br/>";
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,
HERE的操作码echo "Hello", "World!", "<br/>";
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
在第二个和第三个示例中使用了一个额外的操作。不过,在这些示例中,性能(读取速度)几乎可以忽略不计。
点运算符是最快的。您可以通过将其置于循环中并定时输出来测试它,如下所示:
http://www.sitepoint.com/high-performance-string-concatenation-in-php/
第 2 行和第 3 行是等效的。
使用逗号会稍微快一些,因为它没有使用连接。