Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
foreach on my machine.
#!/usr/bin/perl use warnings; use strict; use Benchmark qw(:all); my @bigarray; for(1..100000) { push @bigarray, "A randomish string for testing arrays with realisti +c data" ; } timethese(100, { while_shift => sub{ my @array = @bigarray; while (my $value=shift @array){} }, while_pop => sub{ my @array = @bigarray; while (my $value=pop @array){} }, for_each => sub{ my @array = @bigarray; foreach my $value (@array){} }, for_loop => sub{ my @array = @bigarray; for (my $i=0;$i<=@array;$i++) { my $value=$array[$i]; }}, });
Gives me:
$ perl Benchmark: timing 100 iterations of for_each, for_loop, while_pop, whi +le_shift... for_each: 4 wallclock secs ( 3.85 usr + 0.01 sys = 3.86 CPU) @ 25 +.91/s (n=100) for_loop: 7 wallclock secs ( 7.36 usr + 0.01 sys = 7.37 CPU) @ 13 +.57/s (n=100) while_pop: 5 wallclock secs ( 5.35 usr + 0.00 sys = 5.35 CPU) @ 18 +.69/s (n=100) while_shift: 5 wallclock secs ( 5.38 usr + 0.00 sys = 5.38 CPU) @ 1 +8.59/s (n=100)
Linux, perl, punk rock, cider:

In reply to Re: How to traverse an array as fast as possible? by ciderpunx
in thread How to traverse an array as fast as possible? by llancet

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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?

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

    How do I use this?Last hourOther CB clients
    Other Users?
    Others about the Monastery: (3)
    As of 2024-07-14 18:53 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found

      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.