note
BrowserUk
<blockquote><i></i></blockquote>
<p>You caught that I use <c>1;</c> as the body of the loop to avoid confusing things with the efficiency of the system null device driver (not particularly efficient on Win32). But you missed the point I was trying to make; namely that you cannot use the C-style [for] as a modifier; which means you must always pay the penalty of creating a new scope for each iteration of the loop.
<p>Add that to the benchmark to see that cost:<code>
#! perl
use strict;
use warnings;
#use IO::Null;
use Benchmark qw( cmpthese );
my $n = 1e4;
#my $null = IO::Null->new;
cmpthese 10000, {
foreach_loop => \&foreach_loop,
foreach_modifier => \&foreach_modifier,
c_for_loop => \&c_for_loop,
};
sub foreach_loop {
for my $i (-$n .. 0) {
# $null->print( -$i );
1;
}
}
sub foreach_modifier {
1 for -$n .. 0;
}
sub c_for_loop {
for (my $j = $n; $j >= 0; --$j) {
# $null->print( $j );
1;
}
}
__END__
C:\test>junk
Rate c_for_loop foreach_loop foreach_modifier
c_for_loop 922/s -- -41% -52%
foreach_loop 1573/s 71% -- -18%
foreach_modifier 1922/s 108% 22% --
</code>
<p>Of course, some people eschew the use of modifier forms; but then they are probably the same people that favor their own one-off developer time over the every-user, every-time runtime costs.
<P>As for much of my work I am both user and developer, I don't have that luxury.
<div class="pmsig"><div class="pmsig-171588">
<hr />
<font size=1 >
<div>With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'</div>
<div>Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.</div>
<div>"Science is about questioning the status quo. Questioning authority". </div>
<div>In the absence of evidence, opinion is indistinguishable from prejudice.
</div>
</font>
</div></div>
1021418
1021516