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:*/
    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

Reply via email to