Page 1 of 2

Coloring Even And Odd Rows, What's the best approach?

Posted: Wed Oct 25, 2006 10:47 am
by Cameri
What is the best approach to determine if the row is even or odd?
The most popular way I think it's checking it this way:

Code: Select all

while (...) {
  if ($row_number % 2) {
    //odd
  } else {
    //even
  }
}
But is the popular the best one?

Another way it could be done is this:

Code: Select all

$i=0; // or =1, depends on you
while (...) {
 $i ^= 1;
  if ($i) {
    //even
  } else {
    //odd
  }
}
Another third way:

Code: Select all

$even = true;
while (...)
  if ($even) {
    //even
  } else {
    //odd
  }
  $even = !$even;
}
And so on...

Posted: Wed Oct 25, 2006 11:05 am
by wtf
way I usually go about it is

Code: Select all

$class = ( $i % 2 == 0 ) ? $red : $blue;

<tr class="<?php echo $class; ?>" ...


but I'm also curious about what others are doing

Posted: Wed Oct 25, 2006 2:49 pm
by feyd
Due to the need to sometimes do more elaborate coloring than plain zebra (every other), I tend to use the modulo operator.

Posted: Wed Oct 25, 2006 3:51 pm
by timvw
I generally use the modulo operator too... But in situations where absolute performance is required i might compare the counter with the number, and if they're even, reset the counter to 0...

Posted: Wed Oct 25, 2006 4:28 pm
by Burrito
timvw wrote:But in situations where absolute performance is required i might compare the counter with the number, and if they're even, reset the counter to 0...
wouldn't a boolean be faster than that?

Posted: Wed Oct 25, 2006 6:23 pm
by Ambush Commander
Look, in the end, the database/file-system calls are going to weigh a lot more than a modulus operator called fifty times for a nicely colored table. Just use whatever is most readable and comfortable for you.

Posted: Wed Oct 25, 2006 9:57 pm
by Burrito
Ambush Commander wrote:Look, in the end, the database/file-system calls are going to weigh a lot more than a modulus operator called fifty times for a nicely colored table. Just use whatever is most readable and comfortable for you.
I have no doubt that the bottleneck would not be in the row coloring, I asked if the boolean would be faster simply for my own knowledge's sake....I don't know and want to know.

Posted: Wed Oct 25, 2006 10:02 pm
by Ambush Commander
Sorry, I wasn't trying to directly knock you, I was trying to discuss the thread as a whole.

But while we're on a theoretical basis: no, because once you incremented the variable, it would have to be cast back into an integer. Edit Wait, I think I'm misunderstanding you. What do you mean by using a boolean?

Posted: Wed Oct 25, 2006 10:08 pm
by s.dot
I think i've done something like this in the past..

Code: Select all

$color1 = '#FFFFFF';
$color2 = '#000000';
$row_color = true;

while(something)
{
   if($row_color) echo $color1;  else  echo $color2; 
   $row_color = !$row_color;
}

Posted: Wed Oct 25, 2006 10:13 pm
by Ambush Commander
I'm not sure what timvw is referring to, but is he talking about something like:

Code: Select all

$row = 0;
while ( ... ) {
  $even = $row % 2;
  if ($even) {
     $row = 0;
     // ...
  } else {
    // ...
  }
}
Which, if you really think about it, doesn't seem to have any positive performance effect...

Posted: Wed Oct 25, 2006 10:15 pm
by RobertGonzalez
Booleans baby...

Code: Select all

<?php
$colorizationizer = true;
foreach ($array as $value)
{
    $color = ($colorizationizer) ? '#ff0000' : '#00ff00';
    echo 'The color of ' . $value . ' is ' . $color . '...';
    $colorizationizer = ($colorizationizer) ? false : true;
}
?>

Posted: Wed Oct 25, 2006 10:24 pm
by Cameri
What's the speed increase if you remove the condition and always negate it instead?

Code: Select all

$colorizationizer = !$colorizationizer;

Posted: Wed Oct 25, 2006 11:21 pm
by alex.barylski
Oren had a nifty approach...

Code: Select all

$cnt & 1
Basically you check the first bit, which changes state on every iteration.

As far as efficiency and clarity, assuming you understand how bits in a byte work...that is likely the best approach for alternate coloring...

The modulus operator is probably more expensive (in terms of clock cycles) but it really depends on what you need and what you understand.

Code: Select all

$color = $cnt & 1 ? 'black' : 'white';
echo '<div style="background-color: '.$color.'"></div>';
Cheers :)

Posted: Wed Oct 25, 2006 11:28 pm
by alex.barylski
Burrito wrote:
timvw wrote:But in situations where absolute performance is required i might compare the counter with the number, and if they're even, reset the counter to 0...
wouldn't a boolean be faster than that?
Likely yes. The modulus operator takes two numbers and divides them, returning the remainder...

Anything division on a CPU even with a dedicated FPU is a slow(er) process than whole number calculations. Boolean at the lowest level is typically an integer, so yes, in theory it would be faster.

Posted: Thu Oct 26, 2006 12:08 am
by RobertGonzalez
Cameri wrote:What's the speed increase if you remove the condition and always negate it instead?

Code: Select all

$colorizationizer = !$colorizationizer;
I bet it would increase. Instead of ternary checks a simple 'make it opposite' would be faster I'd guess.