<?xml version="1.0" encoding="windows-1252"?>
<node id="1021525" title="Re^2: counting backward" created="2013-03-03 14:15:23" updated="2013-03-03 14:15:23">
<type id="11">
note</type>
<author id="171588">
BrowserUk</author>
<data>
<field name="doctext">
&lt;blockquote&gt;&lt;i&gt;&lt;/i&gt;&lt;/blockquote&gt;
&lt;p&gt;Cute! But ...

&lt;p&gt;It forces the use of post increment which is more costly that pre increment. And there is no need to explicitly compare to 0. 

&lt;p&gt;Avoiding those and it comes out quicker than the block form of foreach:&lt;code&gt;
#! perl
use strict;
use warnings;
#use IO::Null;
use Benchmark qw( cmpthese );

my $n = 1e4;
#my $null = IO::Null-&gt;new;
cmpthese 10000, {
    foreach_loop =&gt; \&amp;foreach_loop,
    foreach_modifier =&gt; \&amp;foreach_modifier,
    c_for_loop   =&gt; \&amp;c_for_loop,
    c_for_loop2   =&gt; \&amp;c_for_loop2,
    c_for_loop3   =&gt; \&amp;c_for_loop3,
    c_for_loop4   =&gt; \&amp;c_for_loop4,
};

sub foreach_loop {
    for my $i (-$n .. 0) {
        # $null-&gt;print( -$i );
        1;
    }
}

sub foreach_modifier {
    1 for -$n .. 0;
}

sub c_for_loop {
    for (my $j = $n; $j &gt;= 0; --$j) {
        # $null-&gt;print( $j );
        1;
    }
}

sub c_for_loop2 {
    for (my $j = $n; $j-- &gt; 0; ) {
        # $null-&gt;print( $j );
        1;
    }
}

sub c_for_loop3 {
    for (my $j = $n; --$j &gt; 0; ) {
        # $null-&gt;print( $j );
        1;
    }
}

sub c_for_loop4 {
    for (my $j = $n; --$j; ) {
        # $null-&gt;print( $j );
        1;
    }
}

__END__
C:\test&gt;junk
                   Rate c_for_loop2 c_for_loop c_for_loop3 foreach_loop c_for_loop4 foreach_modifier
c_for_loop2       807/s          --       -13%        -38%         -50%        -53%             -56%
c_for_loop        926/s         15%         --        -29%         -43%        -46%             -50%
c_for_loop3      1298/s         61%        40%          --         -19%        -24%             -29%
foreach_loop     1612/s        100%        74%         24%           --         -6%             -12%
c_for_loop4      1707/s        111%        84%         31%           6%          --              -7%
foreach_modifier 1839/s        128%        99%         42%          14%          8%               --
&lt;/code&gt;

&lt;p&gt;But still the modifier form wins.

&lt;div class="pmsig"&gt;&lt;div class="pmsig-171588"&gt;
&lt;hr /&gt;
&lt;font size=1 &gt;
&lt;div&gt;With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'&lt;/div&gt;
&lt;div&gt;Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.&lt;/div&gt;
&lt;div&gt;"Science is about questioning the status quo. Questioning authority". &lt;/div&gt;
&lt;div&gt;In the absence of evidence, opinion is indistinguishable from prejudice.
&lt;/div&gt;
&lt;/font&gt;

&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1021418</field>
<field name="parent_node">
1021524</field>
</data>
</node>
