为什么PHP的转换和比较比is_ *更快?

PHP中优化功能的同时,我改变了
if(is_array($obj)) foreach($obj as $key=>$value { [snip] } 
else if(is_object($obj)) foreach($obj as $key=>$value { [snip] }

if($obj == (array) $obj) foreach($obj as $key=>$value { [snip] } 
else if($obj == (obj) $obj) foreach($obj as $key=>$value { [snip] }

在了解了===之后,我改变了

if($obj === (array) $obj) foreach($obj as $key=>$value { [snip] } 
else if($obj === (obj) $obj) foreach($obj as $key=>$value { [snip] }

将每个测试从is_ *更改为投影导致重大加速(> 30%).

我明白===比==快,因为没有强制必须完成,但是为什么要调用变量比调用任何一个_ *函数要快得多?

编辑:
由于每个人都询问正确性,我写了这个小测试:

$foo=(object) array('bar'=>'foo');
$bar=array('bar'=>'foo');

if($foo===(array) $foo) echo '$foo is an array?';
if($bar===(object) $bar) echo '$bar is an object?';

它不会打印任何错误,并且两个变量都不会改变,所以我认为它是正常工作,但是我已经准备好相信它.

另一个编辑:
Artefacto的程序给了我以下数字:

PHP 5.3.2-1ubuntu4.2 (64bit) on a Core i5-750 with Xdebug
Elapsed (1): 0.46174287796021 / 0.28902506828308
Elapsed (2): 0.52625703811646 / 0.3072669506073
Elapsed (3): 0.57169318199158 / 0.12708187103271
Elapsed (4): 0.51496887207031 / 0.30524897575378
Speculation: Casting and comparing can be about 1.7-4 times faster.
PHP 5.3.2-1ubuntu4.2 (64bit) on a Core i5-750 without Xdebug
Elapsed (1): 0.15818405151367 / 0.214271068573
Elapsed (2): 0.1531388759613 / 0.25853085517883
Elapsed (3): 0.16164898872375 / 0.074632883071899
Elapsed (4): 0.14408397674561 / 0.25812387466431
Without Xdebug,the extra function call didn't matter anymore,so every test (except 3) ran faster.
PHP 5.3.2-1ubuntu4.2 on a Pentium M 1.6GHz
Elapsed (1): 0.97393798828125 / 0.9062979221344
Elapsed (2): 0.39448714256287 / 0.86932587623596
Elapsed (3): 0.44513893127441 / 0.23662400245667
Elapsed (4): 0.38685202598572 / 0.82854390144348
Speculation: Casting an array is slower,casting an object can be faster,but might not be slower.
PHP 5.2.6-1+lenny8 on a Xeon 5110
Elapsed (1): 0.273758888245 / 0.530702114105
Elapsed (2): 0.276469945908 / 0.605964899063
Elapsed (3): 0.332523107529 / 0.137730836868
Elapsed (4): 0.267735004425 / 0.556323766708
Speculation: These results are similar to Artefacto's results,I think it's PHP 5.2.

解决方案:
我使用的分析器(Xdebug)使函数调用速度慢了3倍(即使没有分析),但并没有影响到转换和比较显着,所以铸造和比较看起来更快,即使它不受调试器/分析器.

我不能真正重现.事实上,除了一种情况,你的策略给了我更多的时间:
<?PHP

class A {
    private $a = 4;
    private $b = 4;
    private $f = 7;
}

$arr = array("a" => 4,"b" => 4,"f" => 7);

$obj = new A();

$t = microtime(true);

for ($i = 0; $i < 500000; $i++) {
    is_array($obj) and die("err");
}

echo "Elapsed (1.1): " . (microtime(true) - $t);
echo "\n";

$t = microtime(true);

for ($i = 0; $i < 500000; $i++) {
    ($obj === (array) $obj) and die("err");
}

echo "Elapsed (1.2): " . (microtime(true) - $t);
echo "\n";

$t = microtime(true);

for ($i = 0; $i < 500000; $i++) {
    is_object($arr) and die("err");
}

echo "Elapsed (2.1): " . (microtime(true) - $t);
echo "\n";

$t = microtime(true);

for ($i = 0; $i < 500000; $i++) {
    ($arr === (object) $arr) and die("err");
}

echo "Elapsed (2.2): " . (microtime(true) - $t);
echo "\n";

$t = microtime(true);

for ($i = 0; $i < 500000; $i++) {
    is_object($obj) or die("err");
}

echo "Elapsed (3.1): " . (microtime(true) - $t);
echo "\n";

$t = microtime(true);

for ($i = 0; $i < 500000; $i++) {
    ($obj === (object) $obj) or die("err");
}

echo "Elapsed (3.2): " . (microtime(true) - $t);
echo "\n";

$t = microtime(true);

for ($i = 0; $i < 500000; $i++) {
    is_array($arr) or die("err");
}

echo "Elapsed (4.1): " . (microtime(true) - $t);
echo "\n";

$t = microtime(true);

for ($i = 0; $i < 500000; $i++) {
    ($arr === (array) $arr) or die("err");
}

echo "Elapsed (4.2): " . (microtime(true) - $t);

输出

Elapsed (1.1): 0.366055965424
Elapsed (1.2): 0.550662994385
Elapsed (2.1): 0.337422132492
Elapsed (2.2): 0.579686880112
Elapsed (3.1): 0.402997970581
Elapsed (3.2): 0.190818071365
Elapsed (4.1): 0.332742214203
Elapsed (4.2): 0.549873113632

演员和比较只是检查某物是否是一个对象的速度更快.推测如下:或许因为对象身份检查只需要确定处理程序表和对象句柄是否相同,而在最坏的情况下检查数组标识需要比较所有值.

相关文章

Hessian开源的远程通讯,采用二进制 RPC的协议,基于 HTTP 传输。可以实现PHP调用Java,Python,C#等多语...
初识Mongodb的一些总结,在Mac Os X下真实搭建mongodb环境,以及分享个Mongodb管理工具,学习期间一些总结...
边看边操作,这样才能记得牢,实践是检验真理的唯一标准.光看不练假把式,光练不看傻把式,边看边练真把式....
在php中,结果输出一共有两种方式:echo和print,下面将对两种方式做一个比较。 echo与print的区别: (...
在安装好wampServer后,一直没有使用phpMyAdmin,今天用了一下,phpMyAdmin显示错误:The mbstring exte...
变量是用于存储数据的容器,与代数相似,可以给变量赋予某个确定的值(例如:$x=3)或者是赋予其它的变...