Re: :( This fact ruins my understanding of php :( please explain!
| From: | Markas | Date: | Wed, 09 Oct 2002 07:53:01 +0000 |
| Subject: | Re: :( This fact ruins my understanding of php :( please explain! | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-119396@lists.php.net to get a copy of this message | ||
Thanks for ur interest.
Seairth Jacobs <seairth@seairth.com> wrote in message
news:20021009015338.4579.qmail@pb1.pair.com...
> I have a couple thoughts...
>
> 1) In some other languages, passing an array by value only passes the
first
> element value, not the whole array. This may be happening here, which
would
> explay the fast execution times. I don't know this for sure, but it could
> easily be tested by seeing what's in $a inside the function f().
>
> 2) Try returning the array by reference...
>
> function &f(&$a){return $a;}
>
> for ($i=0; $i < 100; $i++){$a = & f($big);}
>
> ---
> Seairth Jacobs
> seairth@seairth.com
>
>
> "Markas" <mark@e-net.lt> wrote in message
> news:20021008234646.41097.qmail@pb1.pair.com...
> > 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
> > microtime($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?
> >
> >
> >
> >
> >
> >
> >
>
>