在PHP中,通过&改变变量的值,效率如何呢,下面来测试一下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| $arr = ['id' => null, 'name' => 'test', 'age' => null];
$time1 = microtime(true); $tmp = &$arr; for ($i = 0; $i < 100000000; $i++) { $tmp['id'] = $i; } print_r($arr); echo PHP_EOL; echo microtime(true) - $time1;
echo PHP_EOL; $time2 = microtime(true); $tmp1 = null; for ($i = 0; $i < 100000000; $i++) { $tmp1 = $i; } $tmp['id'] = $tmp1; print_r($arr); echo PHP_EOL; echo microtime(true) - $time2;
|
输出结果为:
Array ( [id] => 99999999 [name] => test [age] => )
2.0506858825684
Array ( [id] => 99999999 [name] => test [age] => )
1.1653530597687
不难看出,两次的处理结果是一样的,但通过&多次改变值,效率较低。