$array = range(1, 10000);function test1($array){ return $array;}function &test2($array){ return $array;}$start = microtime(true);for ($i=0; $i < 10000; $i++) { $arr = test1($array); $arr[$i] = 'new';}$end = microtime(true);echo "Cost ".($end - $start)."\n";$start = microtime(true);for ($i=0; $i < 10000; $i++) { $arr = test2($array); $arr[$i] = 'new';}$end = microtime(true);echo "Cost ".($end - $start)."\n";
Output Result:
Cost 5.5163149833679
Cost 6.3323628902435
Although the gap is not big
Reply content:
$array = range(1, 10000);function test1($array){ return $array;}function &test2($array){ return $array;}$start = microtime(true);for ($i=0; $i < 10000; $i++) { $arr = test1($array); $arr[$i] = 'new';}$end = microtime(true);echo "Cost ".($end - $start)."\n";$start = microtime(true);for ($i=0; $i < 10000; $i++) { $arr = test2($array); $arr[$i] = 'new';}$end = microtime(true);echo "Cost ".($end - $start)."\n";
Output Result:
Cost 5.5163149833679
Cost 6.3323628902435
Although the gap is not big
The copy needs to open up space for initialization and so on, equivalent to creating several new objects.
And a reference is just a pointer.
First, according to the PHP: reference return-Manual explanation, the above two kinds of writing is no different, you can understand that the function without & the underlying engine will be optimized, so it may indeed be a little bit faster, so the manual is not recommended.