How is speed affected in the following scenarios:
1) Using a number of if statements i.e. while ($foo) { do_1() if ($condition_1); do_2() if ($condition_2); # ... }
Every condition of this must be tested ever time through, so yes, it's the slowest. It also doesn't mean exactly the same as the other versions, though they may be interchangeable in many cases. Consider:
my $x = 10;
# and then ...
$x++ if $x >= 2; $X++ if $x >= 4; # $x == 12
# or ...
if ($x >= 2) { $x++; } elsif ($x >=4) { $x++; } # $x == 11
2) Using a series of if elsifs i.e. while ($foo) { if ($condition_1) { do_1(); } elsif ($condition_2) { do_2(); } # ... } 3) using a series of if elsifs and calling next: i.e. while ($foo) { if ($condition_1) { do_1(); next; } elsif ($condition_2) { do_2(); next; } # ... }
These two are functionally equivalent IF there is no code in the while loop after the if statements. If there is code down there, it's skipped by the next calls.
For readability, I prefer 1 over 2. But I figured 2 would be faster
then 1. For readability I prefer 2 over 3, but I figure the lasts would
be implied in #2. Is this the case? Or should I bite the bullet and
use 3?
My opinion is that speed probably shouldn't be a factor at all. We're comparing fractional seconds, right? Both are faster that the user blinks, most likely.
I would stress that you should use the one that properly conveys your intent for the code. That would be my top goal. Use ifs/eslifs if you are testing a series of conditions where it's one OR the other. Use all ifs if it could be this condition AND this condition. Use next() if you are trying to bypass something.
Hope that helps.
James
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]