But you are not measuring what you think you are! Once again, the high number of iterations per second Benchmark is reporting should ring many bells and whistles. Do you really think your computer is able to do more than 3 billion modulus operations per second? Because if you believe your Benchmark results, it's grepping through a 1000001 element array 315978.92 times a second. Of course it isn't. You have my @array, which makes that @array is only visible from the file. And not from an eval that's happening in Benchmark.pm (that would defeat all the purposes of my, wouldn't it?), Changing the last lines to:
our @array = (0..1000000); timethese(-10, { 'Step 1' => 'step1(500000, @::array)', 'Step 2' => 'step2(500000, @::array)', 'Step 3' => 'step3(500000, @::array)' });

we get far more sensible results:

Benchmark: running Step 1, Step 2, Step 3 for at least 10 CPU seconds. +.. Step 1: 10 wallclock secs (10.17 usr + 0.00 sys = 10.17 CPU) @ 5.1 +1/s (n=52) Step 2: 10 wallclock secs (10.00 usr + 0.00 sys = 10.00 CPU) @ 5.1 +0/s (n=51) Step 3: 11 wallclock secs (10.72 usr + 0.00 sys = 10.72 CPU) @ 0.4 +7/s (n=5)

Two things can be learned. First, don't blindly use my. Think whether it's appropriate. Second, always distrust your code if Benchmark returns many iterations per second - especially if you do lots of work during an iteration.

Abigail


In reply to Re: Stepping through an array by Abigail-II
in thread Stepping through an array by rob_au

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!
  • 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, details, 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, summary, 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.