Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re^3: counting backward

by Athanasius (Chancellor)
on Mar 04, 2013 at 02:20 UTC ( #1021565=note: print w/replies, xml ) Need Help??

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

vagabonding electron,

You are correct that a foreach loop is faster than a C-style for loop, other things being equal. As BrowserUk has explained, this is because with the latter, but not the former,

you must always pay the penalty of creating a new scope for each iteration of the loop.

But my point was that if you count upwards from -$n, you add the overhead of a unary negation operator when you come to actually use the original $n:

#! perl use strict; use warnings; use Benchmark qw( cmpthese ); my $n = 1e6; my $foreach_loop_total = 0; my $c_for_loop_total = 0; cmpthese ( 1000, { foreach_loop => \&foreach_loop, c_for_loop => \&c_for_loop, } ); print "\$foreach_loop_total = $foreach_loop_total\n"; print "\$c_for_loop_total = $c_for_loop_total\n"; sub foreach_loop { $foreach_loop_total += -$_ for -$n .. 0; } sub c_for_loop { for (my $j = $n; $j >= 0; --$j) { $c_for_loop_total += $j; } }


12:00 >perl Rate foreach_loop c_for_loop foreach_loop 5.62/s -- -8% c_for_loop 6.13/s 9% -- $foreach_loop_total = 500000500000000 $c_for_loop_total = 500000500000000 12:05 >

So, it appears that the overhead of the additional negation outweighs the benefit of not having to create a new scope on each iteration.

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^4: counting backward
by BrowserUk (Pope) on Mar 04, 2013 at 03:28 UTC

    There are still plenty more ways to skin that cat :)

    Why subtract and add; when you can just subtract. And why compare to zero when it is unnecessary:

    #! perl use strict; use warnings; use Benchmark qw( cmpthese ); my $n = 1e6; my $foreach_loop_total = 0; my $c_for_loop_total = 0; my $while_loop_total = 0; my $while_loop_total2 = 0; cmpthese 100, { foreach_loop => \&foreach_loop, c_for_loop => \&c_for_loop, while_loop => \&while_loop, while_loop2 => \&while_loop2, }; print "\$foreach_loop_total = $foreach_loop_total\n"; print "\$c_for_loop_total = $c_for_loop_total\n"; print "\$while_loop_total = $while_loop_total\n"; print "\$while_loop_total2 = $while_loop_total2\n"; sub foreach_loop { $foreach_loop_total -= $_ for -$n .. 0; } sub c_for_loop { for (my $j = $n; $j >= 0; --$j) { $c_for_loop_total += $j; } } sub while_loop { my $n = $n; $while_loop_total += $n-- while $n; } sub while_loop2 { use integer; my $n = $n; $while_loop_total2 += $n-- while $n; } __END__ C:\test>junk Rate c_for_loop while_loop while_loop2 foreach_loo +p c_for_loop 4.94/s -- -22% -39% -44 +% while_loop 6.31/s 28% -- -22% -28 +% while_loop2 8.07/s 63% 28% -- -8 +% foreach_loop 8.76/s 77% 39% 8% - +- $foreach_loop_total = 50000050000000 $c_for_loop_total = 50000050000000 $while_loop_total = 50000050000000 $while_loop_total2 = 50000050000000

    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://1021565]
[erix]: 1=0 is as short as TOP :)
[erix]: "code of someone that died" -- kinda nice if your code stops working too
[erix]: hard to implement, hmm
[Corion]: erix: Well, they also seem to have changed the server, or some software, or whatever, and seem to be in the process of changing the DB schema from having the "username" as primary key to something else.
[Corion]: Far too many things being done at once, or maybe only now has it become apparent that nobody knows that piece of software anymore
[marto]: good morning all

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2017-01-23 09:35 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (192 votes). Check out past polls.