I\'ve just found something very strange in PHP.
If I pass in a variable to a function by reference, and then call a function on it, it\'s incredibly
So, taking your answer already given, you can partially avoid this issue by forcing the copy before iterative work (Copying back afterward if the data is changed).
";
}
function TestCount(&$aArray)
{
$aArray = range(0, 100000);
$fStartTime = microtime(true);
for ($iIter = 0; $iIter < 1000; $iIter++)
{
$iCount = count($aArray);
}
$fTaken = microtime(true) - $fStartTime;
print "took $fTaken seconds\n
";
}
function TestCountA(&$aArray)
{
$aArray = range(0, 100000);
$fStartTime = microtime(true);
$bArray = $aArray;
for ($iIter = 0; $iIter < 1000; $iIter++)
{
$iCount = count($bArray);
}
$aArray = $bArray;
$fTaken = microtime(true) - $fStartTime;
print "A took $fTaken seconds\n
";
}
$nonArray = array();
TestCountNon($nonArray);
$aArray = array();
TestCount($aArray);
$bArray = array();
TestCountA($bArray);
?>
Results are:
Non took 0.00090217590332031 seconds
took 17.676940917969 seconds
A took 0.04144287109375 seconds
Not quite as good, but a damn lot better.