in reply to Efficient Looping Constructs
The inappropriate loop structure I see the most is the 3 argument for. Any time I see this being used to iterate over numbers from $lower to $higher I assume the author is inexperienced in the Perl world, when its an experienced member of the community I basically wonder what they were thinking.
So, if I use for(my $i = 0; $i < 100; $i++){...} your assumption is that I am either inexperienced with Perl or perhaps not thinking clearly?
Just what is the penalty for using the 'for(;;)' version? I've seen your benchmark, let's look at another that compares differences between the two constructs using both lexicals and globals for the summation variable (caps for globals):
use Benchmark qw/cmpthese/; my $lim = 1000; our($SUM1, $SUM2); my($sum1, $sum2); cmpthese(-3,{ 'for' => sub{$sum1 = 0; for(my $i = 1; $i <= $lim; $i++){$sum1 ++=$i}}, 'FOR' => sub{$SUM1 = 0; for(my $i = 1; $i <= $lim; $i++){$SUM1 ++=$i}}, 'foreach' => sub{$sum2 = 0; for my $i (1 .. $lim){$sum2+=$i}}, 'FOREACH' => sub{$SUM2 = 0; for my $i (1 .. $lim){$SUM2+=$i}}, }); __END__ OUTPUT: with absolute rates removed: with $lim = 10: FOR FOREACH foreach for FOR -- -15% -16% -34% FOREACH 18% -- -0% -22% foreach 18% 0% -- -22% for 51% 28% 27% -- with $lim = 1000: FOR for FOREACH foreach FOR -- -42% -44% -46% for 73% -- -3% -6% FOREACH 78% 3% -- -4% foreach 84% 7% 4% --
Now, with a limit of 10 I'd conclude that a plain 'for' loop is best, some 20% faster than the equivalent 'foreach' loop. Why would anyone choose to take a 20% performance hit using foreach? But wait, upping the limit to 1000 and the difference between 'for' and 'foreach' is very marginal (6%, and in the other direction). The big difference here is between the global 'FOR' version and the others. What if I'd only tested the global versions --- then I'd conclude that the 'foreach' version is nearly twice as fast as the equivelant 'for' version. However, as I nearly always use lexicals in real life, this would be an erroneous conclusion. Maybe one *really* is relatively more efficient than the other in some situations --- but which one, and when, is not as obvious as your benchmark makes it seem. I'm sure someone else can construct benchmarks that show completely different relative efficiencies.
Thus far we've only talked of 'relative' efficiency, and a difference approaching 50% might seem huge. However, let's look at absolute differences: in your benchmark you reported absolute rates of 80124/s (foreach) vs 59526/s (for) (using global versions). That amounts to a speed difference around 0.000004 seconds between loops. That's a lot of loops before an application would even stand to gain a half a second.
So, just what is the penalty of using 'for(;;)' as a simple counting loop? Seems to me that the most *noticeable* penalty is the potential to be judged "unPerlish". I guess I'd be more comfortable working with someone who uses 'for(;;)' loops than with someone who makes a big deal about not using them.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Efficient Looping Constructs
by dragonchild (Archbishop) on Oct 01, 2001 at 17:08 UTC | |
by danger (Priest) on Oct 02, 2001 at 01:26 UTC | |
by dragonchild (Archbishop) on Oct 02, 2001 at 19:42 UTC | |
by danger (Priest) on Oct 03, 2001 at 00:36 UTC | |
by dragonchild (Archbishop) on Oct 03, 2001 at 00:57 UTC |