在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倍(即使没有分析),但是没有明显影响转换和比较,所以转换和比较看起来更快,即使它不受影响调试器/分析器。
答案 0 :(得分:4)
我无法真正重现。事实上,除了一个案例之外,你的策略给我的时间更长:
<?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
对于检查某些东西是否为对象,强制转换和比较只会更快。推测如下:也许是因为对象身份检查只需要确定处理程序表和对象句柄是否相同,而在最坏的情况下,检查数组标识需要比较所有值。
答案 1 :(得分:3)
答案 2 :(得分:3)
我只想补充说,对于小型数组/对象,强制转换比较速度更快。
for ($i = 0; $i < 10000; $i ++) {
$arr[] = $i;
}
$t = microtime(true);
for ($i = 0; $i < 5000; $i ++) {
is_array($arr);
}
echo "Elapsed: " . (microtime(true) - $t) . "\n";
$t = microtime(true);
for ($i = 0; $i < 5000; $i ++) {
($arr === (array)$arr);
}
echo "Elapsed: " . (microtime(true) - $t) . "\n";
输出:
Elapsed: 0.0487940311432
Elapsed: 9.20055603981