Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I was doing some coding today when I came to a spot where I needed to use an infinite loop. I remember reading about infinite loops in Programming Perl and that the "for" loop is most customary to use in such a situation. Being the skeptical person that I am, I thought, "It may be customary, but how does it Bench!" So I went and I Benchmarked three infinite loops which I had thought of at the time with this code:
#!/usr/bin/perl -w use strict; use Benchmark; timethese([X], { 'while' => sub { $_ = 0; while (1) { $_++; last if ($_ == 1000); + } }, 'for' => sub { $_ = 0; for (;;) { $_++; last if ($_ == 1000); + } }, 'block' => sub { $_ = 0; { $_++; redo if ($_ < 1000); + } } });

And after running this code with several different iterations (being the scientific person that I am), I came up with the following results:

Benchmark: timing 10000 iterations of block, for, while... block: 8 wallclock secs ( 8.13 usr + 0.00 sys = 8.13 CPU) for: 8 wallclock secs ( 7.90 usr + 0.00 sys = 7.90 CPU) while: 8 wallclock secs ( 8.03 usr + 0.00 sys = 8.03 CPU) Benchmark: timing 20000 iterations of block, for, while... block: 16 wallclock secs (16.25 usr + 0.00 sys = 16.25 CPU) for: 16 wallclock secs (15.80 usr + 0.00 sys = 15.80 CPU) while: 16 wallclock secs (16.08 usr + 0.00 sys = 16.08 CPU) Benchmark: timing 30000 iterations of block, for, while... block: 24 wallclock secs (24.37 usr + 0.00 sys = 24.37 CPU) for: 24 wallclock secs (23.69 usr + 0.00 sys = 23.69 CPU) while: 24 wallclock secs (24.08 usr + 0.00 sys = 24.08 CPU) Benchmark: timing 80000 iterations of block, for, while... block: 66 wallclock secs (65.01 usr + 0.00 sys = 65.01 CPU) for: 63 wallclock secs (63.18 usr + 0.00 sys = 63.18 CPU) while: 64 wallclock secs (64.20 usr + 0.00 sys = 64.20 CPU)

And once again, Programming Perl was absolutely right. The "for" loop was not only the customary infinite loop to use, but also the most efficient infinite loop to use. So now, I have been converted from while (1) { ... } to for (;;) { ... } and hopefully others will soon be converted, too.

Zenon Zabinski | zdog | zdog7@hotmail.com

Update: indigo pointed me in the direction of while() down the thread, and as you can see below, I found that while() was indeed faster than for(;;). Soo....


In reply to The Best Infinite Loop by zdog

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (6)
    As of 2014-04-21 12:26 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (495 votes), past polls