This has been explained a few times. PHP does shallow copies, or copy-on-write which means that the data is not actually copied until you change it. That is:
$a = "1234567890"; $b = $a; internally we do not copy the data from $a to $b until you change $b. We you use references we have a bit more work to do as we need to decouple this and indicate that copy-on-write should not be taking place. Basically things are optimized for the most common case. -Rasmus On Tue, 8 Oct 2002, Markas wrote: > I tried some trivial expirements: > > /* here I define an array $big, which I guess would "eat" ~100kb of memory */ > for($i=0; $i<10000; $i++) > { > $big[$i] = "1234567890"; > } > > /* this func only returns the value it gets as a param...*/ > function f($a){return $a;} > > $start = microtime(); > > /* here all the job is done */ > for ($i=0; $i < 100; $i++){$a = f($big);} /* <--- every iteration I just pass $big >array to this func and it simply returns it*/ > > $end = microtime(); > > /* here I find out the time the job above takes to run, similar to the code from the >help:*/ > function getmicrotime($time){ > list($usec, $sec) = explode(" ",$time); > return ((float)$usec + (float)$sec); > } > > $time_start = getmicrotime($start); > $time_end = getmicrotime($end); > $time = $time_end - $time_start; > > echo "Did nothing in $time seconds"; > > So the script above takes on my server ~0.00115 sec, so as far as I understand, it >takes php to copy that $big array which is rather large, at least 100 times... So I >decided to change the function f($a): > function f($a){return $a;} changed to function f(&$a){return $a;}, > as you can see, I only wanted to pass that $a param by reference, without copying >it, so I thought I win in performance and the 100 iterations will work faster, as no >copying of such a large array $big (which is this time going to be passed by >refernce) will be involved,... BUT this case the job took ~3.75093 seconds, which is >3262 times SLOWER !!! I also found out, that while using refernces, the time of job's >run strictly depends on the $big array dimension, and while NOT using references, the >time doesn't depend on that, but I thought just on the contrary. I thought, that >while using references, we dont copy the data and therefore do not depend on that >data amount, but the example above shows just the opposite results... > > What's going on, if anybody gets interested, please explain? > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php