Re: Performance and Function Calls

From: Date: Tue, 18 Mar 2003 21:52:52 +0000
Subject: Re: Performance and Function Calls
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-140163@lists.php.net to get a copy of this message
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" <dread@texas.net> To: "CF High" <noah@newminddesign.com> Cc: <php-general@lists.php.net> 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 dread@texas.net > -- 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. >

« previous php.general (#140163) next »