我为PHP做了以下性能测试。我试图在数组$time: [{++$x}, {$x++}, {$x += 1}, {$x = $x + 1}]
$time = [0, 0, 0, 0];
for($i = 0; $i < 50; ++$i){
$k = 0;
$t = microtime(true);
$x = 0;
for($j = 0; $j < 100; ++$j)
++$x;
$time[$k++] += microtime(true) - $t;
$t = microtime(true);
$x = 0;
for($j = 0; $j < 100; ++$j)
$x++;
$time[$k++] += microtime(true) - $t;
$t = microtime(true);
$x = 0;
for($j = 0; $j < 100; ++$j)
$x += 1;
$time[$k++] += microtime(true) - $t;
$t = microtime(true);
$x = 0;
for($j = 0; $j < 100; ++$j)
$x = $x + 1;
$time[$k++] += microtime(true) - $t;
}
echo '++$x: ', $time[0] * 1000, 'ms<br />';
echo '$x++: ', $time[1] * 1000, 'ms<br />';
echo '$x += 1: ', $time[2] * 1000, 'ms<br />';
echo '$x = $x + 1: ', $time[3] * 1000, 'ms<br />';
我平均得到10次跑步:0.28ms for ++$x and $x++, 0.26ms for $x += 1 and 0.24ms for $x = $x + 1
我认为,++ $ x和$ x ++更快,但在这里它们更慢。我在脚本中是否出错或者这是否真的是PHP 5.5.9的正常性能?