Hey there. Today I wrote a small landmark script to compare the performance of copying variables and linking to them. I expected that creating links to large arrays, for example, would be significantly slower than copying the entire array. Here is my test code:
<?php $array = array(); for($i=0; $i<100000; $i++) { $array[] = mt_rand(); } function recursiveCopy($array, $count) { if($count === 1000) return; $foo = $array; recursiveCopy($array, $count+1); } function recursiveReference($array, $count) { if($count === 1000) return; $foo = &$array; recursiveReference($array, $count+1); } $time = microtime(1); recursiveCopy($array, 0); $copyTime = (microtime(1) - $time); echo "Took " . $copyTime . "s \n"; $time = microtime(1); recursiveReference($array, 0); $referenceTime = (microtime(1) - $time); echo "Took " . $referenceTime . "s \n"; echo "Reference / Copy: " . ($referenceTime / $copyTime);
The actual result I got was that recursiveReference took about 20 times (!) Until recursiveCopy.
Can anyone explain this behavior to PHP?
reference benchmarking php php-internals
fresskoma
source share