Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Some say for(;;) is inefficient, because it's a C-style loop.
Some say {; redo;} is inefficient, because it won't let perl optimize.
Some say while(1) is inefficient, because the 1 has to be evaluated over and oer.

I wanted to know, so I benchmarked. I found it somewhat hard to benchmark infinite loops, so I made them finite with a nice closure construct.

#!/usr/bin/perl -w use strict; use Benchmark; sub ender () { my $i = 0; return sub { ++$i == 10000 } } Benchmark::cmpthese(-5, { 'for (;;)' => sub { my $end = ender(); for (;;) { last if $end->() } }, 'while (1)' => sub { my $end = ender(); while (1) { last if $end->() } }, '{; redo;}' => sub { my $end = ender(); { last if $end->(); redo; } } }); __END__ Benchmark: running for (;;), while (1), {; redo;}, each for at least 5 CPU seconds... for (;;): 9 wsecs (5.48 usr + 0.01 sys = 5.49 CPU) @ 57.56/s (n=316) while (1): 8 wsecs (5.26 usr + 0.01 sys = 5.27 CPU) @ 60.34/s (n=318) {; redo;}: 8 wsecs (5.08 usr + 0.00 sys = 5.08 CPU) @ 57.68/s (n=293) Rate for (;;) {; redo;} while (1) for (;;) 57.6/s -- -0% -5% {; redo;} 57.7/s 0% -- -4% while (1) 60.3/s 5% 5% --


I've tried this many times. while seems to be the winner.

Update (200201041112+0100)
This is really odd. I've run the benchmark above about a hundred times now, and while won almost every one of them.

BUT
2;1 juerd@ouranos:~$ perl -MO=Deparse -e'while (1) { print "1\n" }' for (;;) { print "1\n"; } -e syntax OK 2;0 juerd@ouranos:~$ perl -MO=Deparse -e'for (;;) { print "1\n" }' for (;;) { print "1\n"; } -e syntax OK

Can anyone explain this?

2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$


In reply to Re: do/redo or for(;;): what's Kosher? by Juerd
in thread do/redo or for(;;): what's Kosher? by rje

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-19 12:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found