$ time php -f good.php
real 0m0.013s
user 0m0.005s
sys 0m0.007
不加引号的
:
代码如下
:
$ time php -f bad.php
PHP Notice: Use of undefined constant bad - assumed 'bad'
in
/home/huixinchen/tmp/bad.php
on line (此处省略 999 行 NOTICE)
real 0m0.100s
user 0m0.020s
sys 0m0.029
看看
,差别有多大?
哦
, 或许我们应该模拟一下那些”幸运的”人们的情况, 去掉花费在记录 NOTICE 的开销, 看看
~
代码如下
:
$ time php -f bad.php
real 0m0.037s
user 0m0.018s
sys 0m0.018
我们可以看出
, 基本上, 使用引号,和不使用引号的效率损失在 3 倍以上
那么
, 这些效率损失到哪里去了呢?
我们分别看下
, 俩个文件生成的 OPCODE 序列:
good.php :
代码如下
:
filename: /home/huixinchen/tmp/good.php
compiled vars: !0 = $array, !1 = $i
line # op fetch ext return operands
-------------------------------------------------------------------------------
2 0 INIT_ARRAY ~0
1 ASSIGN !0, ~0
3 2 ASSIGN !1, 0
4 3 PRE_INC $3 !1
4 IS_SMALLER ~4 $3, 1000
5 JMPZ ~4, ->9
5 6 ZEND_ASSIGN_DIM !0, 'good'
7 ZEND_OP_DATA 2, $6
6 8 JMP ->3