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

Reply via email to