gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:38:00 26 4
gpt4 key购买 nike

在优化 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%)。

我知道 ===== 更快,因为不需要进行强制转换,但为什么转换变量比调用任何变量都要快得多is_*-函数?

编辑:既然大家都问正确性,我就写了这个小测试:

$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 XdebugElapsed (1): 0.46174287796021 / 0.28902506828308Elapsed (2): 0.52625703811646 / 0.3072669506073Elapsed (3): 0.57169318199158 / 0.12708187103271Elapsed (4): 0.51496887207031 / 0.30524897575378Speculation: Casting and comparing can be about 1.7-4 times faster.
PHP 5.3.2-1ubuntu4.2 (64bit) on a Core i5-750 without XdebugElapsed (1): 0.15818405151367 / 0.214271068573Elapsed (2): 0.1531388759613 / 0.25853085517883Elapsed (3): 0.16164898872375 / 0.074632883071899Elapsed (4): 0.14408397674561 / 0.25812387466431Without 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.6GHzElapsed (1): 0.97393798828125 / 0.9062979221344Elapsed (2): 0.39448714256287 / 0.86932587623596Elapsed (3): 0.44513893127441 / 0.23662400245667Elapsed (4): 0.38685202598572 / 0.82854390144348Speculation: 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 5110Elapsed (1): 0.273758888245 / 0.530702114105Elapsed (2): 0.276469945908 / 0.605964899063Elapsed (3): 0.332523107529 / 0.137730836868Elapsed (4): 0.267735004425 / 0.556323766708Speculation: 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.366055965424Elapsed (1.2): 0.550662994385Elapsed (2.1): 0.337422132492Elapsed (2.2): 0.579686880112Elapsed (3.1): 0.402997970581Elapsed (3.2): 0.190818071365Elapsed (4.1): 0.332742214203Elapsed (4.2): 0.549873113632

强制转换和比较只是为了检查某物是否是对象而更快。推测如下:也许是因为对象身份检查只需要确定处理程序表和对象句柄是否相同,而检查数组身份在最坏的情况下需要比较所有值。

关于php - 为什么 PHP 中的转换和比较比 is_* 更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2981994/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com