good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re^3: counting backwardby Athanasius (Archbishop) |
on Mar 04, 2013 at 02:20 UTC ( [id://1021565]=note: print w/replies, xml ) | Need Help?? |
You are correct that a foreach loop is faster than a C-style for loop, other things being equal. As BrowserUk has explained, this is because with the latter, but not the former, you must always pay the penalty of creating a new scope for each iteration of the loop. But my point was that if you count upwards from -$n, you add the overhead of a unary negation operator when you come to actually use the original $n:
Output:
So, it appears that the overhead of the additional negation outweighs the benefit of not having to create a new scope on each iteration.
In Section
Seekers of Perl Wisdom
|
|