我做了一个小实验,我得到了一个令人惊讶的结果(至少对我而言)。我已经编写了6个函数来以不同的方式在PHP中总结一个数组。在这里你可以看到结果:
PHP ver: 5.5.36
foreach_by_value(): 1.1668000221252
foreach_by_ref(): 0.38018989562988
array_sum_by_value(): 0.11428999900818
array_sum_by_ref(): 0.91070008277893
for_by_value(): 0.50360298156738
for_by_ref(): 1.2752962112427
正如您所看到的,通过引用传递数组有时会使其在较慢的其他情况下更快。 我真的不知道这背后的原因是什么。
以下是我用于测试的代码:
echo phpversion() . "\n";
const N = 100000;
const REPEAT = 100;
for ($i = 0;$i < N;$i++) {
$array[] = rand(0, 10000);
}
function foreach_by_value($a) {
$sum = 0;
foreach($a as $number) {
$sum += $number;
}
}
function foreach_by_ref(&$a) {
$sum = 0;
foreach($a as $number) {
$sum += $number;
}
}
function array_sum_by_value($a) {
$sum = array_sum($a);
}
function array_sum_by_ref(&$a) {
$sum = array_sum($a);
}
function for_by_value($array) {
$sum = 0;
$count = count($array);
for($i = 0;$i < $count;$i++) {
$sum += $array[$i];
}
}
function for_by_ref(&$array) {
$sum = 0;
$count = count($array);
for($i = 0;$i < $count;$i++) {
$sum += $array[$i];
}
}
$start_time = microtime(true);
for ($i = 0;$i < REPEAT;$i++) {
foreach_by_value($array);
}
$finish_time = microtime(true);
echo "foreach_by_value(): \t" . ($finish_time - $start_time) . "\n";
$start_time = microtime(true);
for ($i = 0;$i < REPEAT;$i++) {
foreach_by_ref($array);
}
$finish_time = microtime(true);
echo "foreach_by_ref(): \t" . ($finish_time - $start_time) . "\n";
$start_time = microtime(true);
for ($i = 0;$i < REPEAT;$i++) {
array_sum_by_value($array);
}
$finish_time = microtime(true);
echo "array_sum_by_value(): \t" . ($finish_time - $start_time) . "\n";
$start_time = microtime(true);
for ($i = 0;$i < REPEAT;$i++) {
array_sum_by_ref($array);
}
$finish_time = microtime(true);
echo "array_sum_by_ref(): \t" . ($finish_time - $start_time) . "\n";
$start_time = microtime(true);
for ($i = 0;$i < REPEAT;$i++) {
for_by_value($array);
}
$finish_time = microtime(true);
echo "for_by_value(): \t" . ($finish_time - $start_time) . "\n";
$start_time = microtime(true);
for ($i = 0;$i < REPEAT;$i++) {
for_by_ref($array);
}
$finish_time = microtime(true);
echo "for_by_ref(): \t\t" . ($finish_time - $start_time) . "\n";
答案 0 :(得分:2)
$number
,因此需要额外的时间; next()
的内部调用都是引用,这需要额外的时间进行重定向(对于每个元素,数组); current()
和next()
,这比 foreach_by_ref需要更多时间强>