Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

(tye)RE: The Best Infinite Loop

by tye (Cardinal)
on Oct 17, 2000 at 10:23 UTC ( #37103=note: print w/ replies, xml ) Need Help??


in reply to The Best Infinite Loop

I looked at those benchmarks and said, "Well, those all took the same amount of time." Then I read the conclusions drawn and frowned. The numbers are way too close together to determine that any of these are faster than the other. In fact, I'd be surprised if the first two don't compile to the exact same parse tree.

Compare my benchmarks:

#!/usr/bin/perl -w use strict; use Benchmark; timethese( 1000, { '0while' => sub { $_ = 0; while (1) { $_++; last if ($_ == 10000 +); } }, '0for' => sub { $_ = 0; for (;;) { $_++; last if ($_ == 10000 +); } }, '0block' => sub { $_ = 0; { $_++; redo if ($_ < 10000) +; } }, '1while' => sub { $_ = 0; while (1) { $_++; last if ($_ == 10000 +); } }, '1for' => sub { $_ = 0; for (;;) { $_++; last if ($_ == 10000 +); } }, '1block' => sub { $_ = 0; { $_++; redo if ($_ < 10000) +; } }, }); __END__ Benchmark: timing 1000 iterations of 0block, 0for, 0while, 1block, 1for, 1while... 0block: 7 wallclock secs ( 6.81 CPU) @ 146.84/s 0for: 7 wallclock secs ( 6.98 CPU) @ 143.27/s 0while: 7 wallclock secs ( 6.70 CPU) @ 149.25/s 1block: 6 wallclock secs ( 6.37 CPU) @ 156.99/s 1for: 6 wallclock secs ( 6.26 CPU) @ 159.74/s 1while: 6 wallclock secs ( 6.32 CPU) @ 158.23/s
Note that the first time for(;;) is the slowest and the second time it is the fastest.

I'm sticking with while(1) since I think it looks nicer.

        - tye (but my friends call me "Tye")


Comment on (tye)RE: The Best Infinite Loop
Select or Download Code
RE: (tye)RE: The Best Infinite Loop
by rdw (Curate) on Oct 17, 2000 at 14:23 UTC

    So I tried it (with Deparse)...

    while () { $_++; last if $_ == 1000; } while (1) { $_++; last if $_ == 1000; } for (;;) { $_++; last if $_ == 1000; }
    perl -MO=Deparse test.pl
    for (;;) { ++$_; last if $_ == 1000; } for (;;) { ++$_; last if $_ == 1000; } for (;;) { ++$_; last if $_ == 1000; } test.pl syntax OK

    So they should be all the same speed. I'm with tye on this one - I'll be sticking with with while(1) since I think it looks nicer.

    Have fun,

    rdw

      Those three parse the same, but the block loops don't parse that way. In fact, there are at least three different ways to do an infinite loop with redo that parse differently... So I modified tye's benchmark, and got:
      #!/usr/bin/perl -w use strict; use Benchmark; timethese( 1000, { '0while' => sub { $_ = 0; while (1) { $_++; last if ($_ == 10000) +; } }, '0for' => sub { $_ = 0; for (;;) { $_++; last if ($_ == 10000) +; } }, '0redo1' => sub { $_ = 0; { $_++; redo if ($_ < 10000); + } }, '0redo2' => sub { $_ = 0; { $_++; redo unless ($_ == 10000); + } }, '0redo3' => sub { $_ = 0; { $_++; last if ($_ == 10000); redo +; } }, '1while' => sub { $_ = 0; while (1) { $_++; last if ($_ == 10000) +; } }, '1for' => sub { $_ = 0; for (;;) { $_++; last if ($_ == 10000) +; } }, '1redo1' => sub { $_ = 0; { $_++; redo if ($_ < 10000); + } }, '1redo2' => sub { $_ = 0; { $_++; redo unless ($_ == 10000); + } }, '1redo3' => sub { $_ = 0; { $_++; last if ($_ == 10000); redo +; } }, }); __END__ Benchmark: timing 1000 iterations of 0for, 0redo1, 0redo2, 0redo3, 0wh +ile, 1for, 1redo1, 1redo2, 1redo3, 1while... 0for: 14 wallclock secs (11.43 usr + 0.00 sys = 11.43 CPU) 0redo1: 14 wallclock secs (11.62 usr + 0.00 sys = 11.62 CPU) 0redo2: 15 wallclock secs (12.68 usr + 0.00 sys = 12.68 CPU) 0redo3: 15 wallclock secs (12.36 usr + 0.00 sys = 12.36 CPU) 0while: 14 wallclock secs (11.48 usr + 0.00 sys = 11.48 CPU) 1for: 13 wallclock secs (11.40 usr + 0.00 sys = 11.40 CPU) 1redo1: 14 wallclock secs (11.63 usr + 0.00 sys = 11.63 CPU) 1redo2: 14 wallclock secs (11.54 usr + 0.00 sys = 11.54 CPU) 1redo3: 15 wallclock secs (12.36 usr + 0.01 sys = 12.37 CPU) 1while: 14 wallclock secs (11.42 usr + 0.00 sys = 11.42 CPU)
      I don't know why the difference in times between the first run and the second...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://37103]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2014-10-23 00:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (123 votes), past polls