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...
-
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.