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.


In reply to Re: Efficient Looping Constructs by danger
in thread Efficient Looping Constructs by demerphq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.