Darn you Phillip! ;) I was just about to post that. Also, since we're being picky, you should also assign the result to $bgcolor in each of your tests like you are the first one, instead of just testing a condition. Here are the results with the fourth method that Phillip mentioned (along with single quotes, etc, to make each test the same):
version one: 0.400743961334 seconds version two: 0.331864953041 seconds version three: 0.322684049606 seconds version four: 0.293377041817 seconds There's really not much a difference between any of them. Use the one you understand. Complete _new_ code included below. ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ <?php function getmicrotime(){ list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); } $ITERATIONS = 100000; //METHOD ONE $time_start1 = getmicrotime(); $bgcolor = ''; for ($i = 1; $i < $ITERATIONS; $i++) { $bgcolor = ($bgcolor == '#E3E8F0') ? '#C7D0E2' : '#E3E8F0'; } $time_end1 = getmicrotime(); $time1 = $time_end1 - $time_start1; echo "version one: \t$time1 seconds<P>\n"; //METHOD TWO $time_start2 = getmicrotime(); $tf = TRUE; for ($i = 1; $i < $ITERATIONS; $i++) { $tf=!$tf; $bgcolor = ($tf) ? '#C7D0E2' : '#E3E8F0'; } $time_end2 = getmicrotime(); $time2 = $time_end2 - $time_start2; echo "version two: \t$time2 seconds<P>\n"; //METHOD THREE $time_start3 = getmicrotime(); $r = 0; for ($i = 1; $i < $ITERATIONS; $i++) { $bgcolor = (($r++ % 2 == 0) ? '#C7D0E2' : '#E3E8F0'); } $time_end3 = getmicrotime(); $time3 = $time_end3 - $time_start3; echo "version three: \t$time3 seconds<P>\n"; //METHOD FOUR $time_start4 = getmicrotime(); $z = 0; for ($i = 1; $i < $ITERATIONS; $i++) { $bgcolor = ((++$z & 1) ? '#C7D0E2' : '#E3E8F0'); } $time_end4 = getmicrotime(); $time4 = $time_end4 - $time_start4; echo "version four: \t$time4 seconds<p>\n"; ?> > -----Original Message----- > From: Philip Olson [mailto:[EMAIL PROTECTED] > Sent: Saturday, April 05, 2003 7:42 PM > To: Daevid Vincent > Cc: [EMAIL PROTECTED] > Subject: RE: [PHP] RE: newbie alternate row colours in dynamic table -- > timed tests > > > If you really want to be optimal, use bitwise instead > of modulus :) And ++$i instead of $i++ :) And, > use single not double quotes :) Silly, but true. > > http://www.faqts.com/knowledge_base/view.phtml/aid/783/fid/9 > > And, write less code, so: > > $bgcolor = (++$i & 1) ? '#ffffff' : '#eeeeee'; > > Regards, > Philip > > > On Sat, 5 Apr 2003, Daevid Vincent wrote: > > > I had to know... ;-) > > > > Output: > > > > version one: 0.56761503219604 seconds > > version two: 0.3099730014801 seconds > > version three: 0.36320495605469 secondss > > > > So the boolean (V2)is faster: > > > > Mine is slightly slower by a 'smidge' (0.06 seconds) > > > > Top one is cleanest but slower. > > > > --- test --- > > > > <?php > > function getmicrotime(){ > > list($usec, $sec) = explode(" ",microtime()); > > return ((float)$usec + (float)$sec); > > } > > > > $ITERATIONS = 100000; > > > > $time_start = getmicrotime(); > > for ($i = 1; $i < $ITERATIONS; $i++) > > { > > $bgcolor = ($bgcolor == '#E3E8F0') ? '#C7D0E2' : '#E3E8F0'; > > } > > $time_end = getmicrotime(); > > $time1 = $time_end - $time_start; > > echo "version one: \t$time1 seconds<P>\n"; > > > > $time_start = getmicrotime(); > > $tf = TRUE; > > for ($i = 1; $i < $ITERATIONS; $i++) > > { > > $tf=!$tf; > > ($tf) ? "'C7D0E2" : "E3E8F0"; > > } > > $time_end = getmicrotime(); > > $time2 = $time_end - $time_start; > > echo "version two: \t$time2 seconds<P>\n"; > > > > $time_start = getmicrotime(); > > $r = 0; > > for ($i = 1; $i < $ITERATIONS; $i++) > > { > > (($r++ % 2 == 0) ? "'C7D0E2" : "E3E8F0"); > > } > > $time_end = getmicrotime(); > > $time3 = $time_end - $time_start; > > echo "version three: \t$time3 seconds<P>\n"; > > ?> > > > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php