问题描述
在PHP中,假设$value = 12345;
(一个整数),当将$value
从整数转换为字符串时,这会更快;
In PHP, assuming $value = 12345;
(an integer), which is faster when casting $value
from an integer to a string;
$value = (string)$value;
或
$value = "$value";
这是一种性能度量问题,专门针对这种情况.感谢您的帮助!
This is a kind of performance measure question and specifically for this case. Thanks for your help!
推荐答案
您的问题实际上是关于php解释器的功效,以及它如何将php code
(您编写的那个)转换为php bytecode
(那个运行并可能实际上消耗时间和资源).如果我接受p01ymath的实验,并分解它:
Your question is really about the efficacy of the php interpreter, and how it converts php code
(the one you write) to php bytecode
(the one that runs and may actually consume time and resources). If i take p01ymath's experiment, and decompose it:
implicit.php
<?php
$str = 12345;
for($i=0;$i<=200000000;$i++){
$str2 = "$str";
}
implicit.php字节码
DarkMax:temp yvesleborg$ php -dvld.active=1 -dvld.verbosity=0 -dvld.exececute=0 implicit.php
filename: /Users/yvesleborg/temp/implicit.php
function name: (null)
number of ops: 14
compiled vars: !0 = $str, !1 = $i, !2 = $str2
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------------------
2 0 E > EXT_STMT
1 ASSIGN !0, 12345
3 2 EXT_STMT
3 ASSIGN !1, 0
4 > JMP ->10
4 5 > EXT_STMT
6 CAST 6 ~5 !0
7 ASSIGN !2, ~5
3 8 POST_INC ~7 !1
9 FREE ~7
10 > IS_SMALLER_OR_EQUAL ~8 !1, 200000000
11 EXT_STMT
12 > JMPNZ ~8, ->5
7 13 > > RETURN 1
branch: # 0; line: 2- 3; sop: 0; eop: 4; out0: 10
branch: # 5; line: 4- 3; sop: 5; eop: 9; out0: 10
branch: # 10; line: 3- 3; sop: 10; eop: 12; out0: 13; out1: 5; out2: 13; out3: 5
branch: # 13; line: 7- 7; sop: 13; eop: 13; out0: -2
path #1: 0, 10, 13,
path #2: 0, 10, 5, 10, 13,
path #3: 0, 10, 5, 10, 13,
path #4: 0, 10, 13,
path #5: 0, 10, 5, 10, 13,
path #6: 0, 10, 5, 10, 13,
explicit.php
<?php
$str = 12345;
for($i=0;$i<=200000000;$i++){
$str2 = (string)$str;
}
explicit.php字节码
explicit.php bytecode
DarkMax:temp yvesleborg$ php -dvld.active=1 -dvld.verbosity=0 -dvld.exececute=0 explicit.php
filename: /Users/yvesleborg/temp/explicit.php
function name: (null)
number of ops: 14
compiled vars: !0 = $str, !1 = $i, !2 = $str2
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------------------
2 0 E > EXT_STMT
1 ASSIGN !0, 12345
3 2 EXT_STMT
3 ASSIGN !1, 0
4 > JMP ->10
4 5 > EXT_STMT
6 CAST 6 ~5 !0
7 ASSIGN !2, ~5
3 8 POST_INC ~7 !1
9 FREE ~7
10 > IS_SMALLER_OR_EQUAL ~8 !1, 200000000
11 EXT_STMT
12 > JMPNZ ~8, ->5
7 13 > > RETURN 1
branch: # 0; line: 2- 3; sop: 0; eop: 4; out0: 10
branch: # 5; line: 4- 3; sop: 5; eop: 9; out0: 10
branch: # 10; line: 3- 3; sop: 10; eop: 12; out0: 13; out1: 5; out2: 13; out3: 5
branch: # 13; line: 7- 7; sop: 13; eop: 13; out0: -2
path #1: 0, 10, 13,
path #2: 0, 10, 5, 10, 13,
path #3: 0, 10, 5, 10, 13,
path #4: 0, 10, 13,
path #5: 0, 10, 5, 10, 13,
path #6: 0, 10, 5, 10, 13,
如您所见,
这两个代码片段都生成完全相同的字节码(您可以从任何精心设计的编译器/解释器中获得期望的字节码).因此,上面的实验仅测量引擎在对字节码进行排序时,在运行时以及在运行时(在芯片组上)在引擎上的实际运行时性能.
as you can see, both snippets produce exactly the same bytecode (which you would expect from any well-crafted compiler/interpreter). Thus, the experiment above merely measures the actual run-time performance of the engine as it is sequencing the bytecode, at the time it was run, and on the box (chipset) it was run on.
要真正回答您自己的问题,您必须考虑棘手的问题:
To really answer your own question, you have to ponder the trickier question:
,如果您发现这种情况,请进行测试以衡量其各自的性能.
and if you find such circumstances, instrument a test to measure their respective performance.
如果您想执行此任务,则需要pecl vld
组件.您可以按照以下 有趣的帖子 来熟悉vld
(请务必检查pecl,并为测试中的php编译器安装适当的版本)
If you want to pursue this quest, you will need the pecl vld
component. You can follow this interesting post to get familiar with vld
(be sure to check on pecl, and install the appropriate version for your php compiler under test)
这篇关于(string)$ value和"$ value"之间比较快转换为字符串时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!