My apologies. You are of course correct. In my defense I can only say that the two argument usage of the three argument for was a hasty addition, in order to be fair, after I noticed that its performance was better in other benchmarks that I did.

I corrected the fragment to the below

'for(;;)2' => '$sum=0; for (my $i=0;$i<=$n;) {$sum+=$i++}'
Which in itself is an instructive point, converting a 3 argument into a while(){} or simply by converting it into the 2 of 3 argment form, achieves a considerable speedup.

However while your point is fair I beleive that you will agree that in this case, luckily, this error did not affect the validity of the timings as the loop still performed the same set of iterations, and the sum was simply present to ensure (perhaps without reason) that perl did not optimize away empty blocks (perhaps inconsistently across the variation). The only difference being that as you said my error meant that that loop calculated 1..$n+1 instead of 0..$n.

Nevertheless should I make a similer post I shall endeavour to ensure that I dont make this type of mistake again.

:-)

Yves
--
You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)


In reply to Re: Re: Efficient Looping Constructs by demerphq
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.