Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^3: counting backward

by BrowserUk (Pope)
on Mar 03, 2013 at 18:40 UTC ( #1021523=note: print w/ replies, xml ) Need Help??

in reply to Re^2: counting backward
in thread counting backward

You caught that I use 1; 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.

Add that to the benchmark to see that cost:

#! 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_modi +fier c_for_loop 922/s -- -41% +-52% foreach_loop 1573/s 71% -- +-18% foreach_modifier 1922/s 108% 22% + --

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.

As for much of my work I am both user and developer, I don't have that luxury.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
Comment on Re^3: counting backward
Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2016-05-27 21:33 GMT
Find Nodes?
    Voting Booth?