Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1021523]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2017-10-22 09:29 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (272 votes). Check out past polls.