+3  A: 

With the inverse for loop, you're only doing one variable lookup per iteration:

$w > 0         // <-- one lookup to the $w variable

$w < $counter  // <-- two lookups, one for $w, one for $counter

This is why the inverse is slightly faster. Also, a while loop has only one operation per iteration:

$w < $counter        // <-- one operation while loop

$w < $counter ; $w++ // <-- two operation for loop

Of course, you have that extra operation inside the loop's code block, but I'm not sure exactly why that's faster (maybe someone can fill in the blank there). You'll notice the time difference is minimal, because these operations are still very fast. Such micro-optimisations are most effective on very large loops.

Andy E
Ah I see. Both explanations make perfect sense. But in the first case, If I set the $counter just to 1000000 It is still one look up, but still the same results.
Saif Bechan
`$counter` has to be looked up at every loop iteration, not just once.
Mike Daniels
Your second case here is a bit off the mark. Whenever/wherever it's done, `$w` still has to be incremented on every iteration.
Chris
@Saif: do you mean if you swap `$counter` with `1000000` the results are the same, or if you set `$counter = 1000000` the results are the same? If it's the former, then I'm not sure. Maybe subtraction is a cheaper operation than addition ;-) If it's the latter, the variable is still looked up on each iteration (because it's a variable that could be changed from inside the loop).
Andy E
@Chris: I did note that, and honestly I'm not sure why it is faster.
Andy E
I ment removing the counter variable and just hard code the number. I get the exact same results
Saif Bechan