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")