Thanks for the informative response, Don.

While a few microseconds saved isn't much, the example I gave was not the
"real_world" situation we're actually dealing with.

We're looping through a roster list of players (e.g. a soccer team) with a
minimum of twenty players on a team.  When filling out the roster, each
player field has a set of drop downs ranging from state and country of
origin to height and weight and jersey #.  By setting the drop downs (which
are generated by functions) to variables before looping through the number
of players on a team, I think we'll save a fair amount of time, not to
mention having to make changes in one place; not throughout the
page............

Alright enough blathering.

Thanks again for your help,

--Noah


----- Original Message -----
From: "Don Read" <[EMAIL PROTECTED]>
To: "CF High" <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Sent: Saturday, March 15, 2003 8:25 PM
Subject: RE: [PHP] Performance and Function Calls


>
> On 15-Mar-2003 CF High wrote:
> > Hey all.
> >
> > Quick question:
> >
> > If I have a function that, say, prints out the months in a year, and I
> > call
> > that function within a 10 cycle loop, which of the following is faster:
> >
> >     1) Have function months() return months as a string; set var
> > string_months = months() outside of the loop; then echo string_months
> > within
> > the loop
> >
> >     -- OR
> >
> >     2) Just call months() for each iteration through the loop
> >
> > I'm not sure how PHP interprets option number 1.
> >
> > Guidance for the clueless much appreciated...........
> >
>
> Easy enuff to test:
>
> <?php
>
> function getmicrotime(){
>     list($usec, $sec) = explode(" ",microtime());
>     return ((float)$usec + (float)$sec);
> }
>
> $time_start = getmicrotime();
> for ($m=1; $m <11; $m++) {
>     echo date('F', strtotime("2003-$m-1")), '<br>';
> }
> echo '<P>A: ', getmicrotime() - $time_start , '<P>';
>
> $time_start = getmicrotime();
> for ($m=1; $m <11; $m++) {
>     $str[]=date('F', strtotime("2003-$m-1"));
> }
> echo implode('<br>',$str);
> echo '<P>B: ', getmicrotime() - $time_start , '<P>';
>
> unset($str);
> $time_start = getmicrotime();
> for ($m=1; $m <11; $m++) {
>     $str[]=date('F', strtotime("2003-$m-1"));
> }
>
> while (list(,$v)= each($str)) {
>     echo $v, '<br>';
> }
> echo '<P>C: ', getmicrotime() - $time_start , '<P>';
>
> ?>
>
> On my machine I get numbers like:
> A: 0.000907063484192
> B: 0.000651001930237
> C: 0.000686049461365
>
> The function call within the loop is slower (contrary to what I
> expected), the real question is how much effort do you want to expend to
> save 2-3 micro-seconds?
>
> Regards,
> --
> Don Read                                       [EMAIL PROTECTED]
> -- It's always darkest before the dawn. So if you are going to
>    steal the neighbor's newspaper, that's the time to do it.
>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to