Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
  • For for (X..Y), Perl uses a counting loop.
  • For for (reverse constX..constY), Perl builds a flattened list at compile-time and iterates over it in reverse.
  • For for (reverse X..Y), Perl builds a flattened list at run-time and iterates over it in reverse.

So far, I've found 6 different kinds of for loops in Perl. You can find them in an earlier node.

#!/usr/bin/perl -- use strict; use warnings; use Benchmark qw(cmpthese); my $BIG = 100_000; cmpthese(-3, { c_f => \&c_f, c_r => \&c_r, p_fv => \&p_fc, p_fc => \&p_fv, p_rc => \&p_rc, p_rv => \&p_rv, }); sub c_f { for (my $i = 0; $i < $BIG; $i++ ) { 1 } } sub c_r { for (my $i = $BIG; $i-- > 0; ) { 1 } } sub p_fc { for my $i ( 0..100_000-1 ) { 1 } } sub p_fv { for my $i ( 0..$BIG-1 ) { 1 } } sub p_rc { for my $i ( reverse 0..100_000-1 ) { 1 } } sub p_rv { for my $i ( reverse 0..$BIG-1 ) { 1 } } __END__ Rate p_rv c_r c_f p_rc p_fv p_fc p_rv 28.5/s -- -37% -44% -59% -64% -65% Builds list and loops. c_r 45.0/s 58% -- -11% -35% -44% -44% c_f 50.7/s 78% 13% -- -27% -37% -37% p_rc 69.7/s 144% 55% 37% -- -13% -14% Loops over pre-built array. p_fv 80.0/s 180% 78% 58% 15% -- -1% Counting loop p_fc 80.7/s 183% 79% 59% 16% 1% -- Counting loop

If you add "()," to the front of any of the lists, you'll notice that p_* will become as slow as p_rv.


In reply to Re^6: C-style for loop by ikegami
in thread C-style for loop by mandog

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (12)
    As of 2015-07-02 10:51 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (34 votes), past polls