Is there a way to set initial conditions for iterations while benchmarking (with Benchmark or anything similar) such that the setup time is excluded from the benchmark?

I have

use strict; use warnings; use Benchmark; my @strings = qw(exception:tex exception:mex asdf tex:exception:mex); Benchmark::cmpthese( -5, { 'one' => sub { my @unfiltered = @strings; my @filtered = grep { /e +xception:(?!tex)/} @unfiltered; }, 'two' => sub { my @unfiltered = @strings; my @filtered = grep { /e +xception/ && !/tex/ } @unfiltered; }, 'three' => sub { my @unfiltered = @strings; my @filtered = grep { +/exception:/g && !/\Gtex/ } @unfiltered; }, });

Each sub begins with "my @unfiltered = @strings" so that they all have the same setup overhead and so that pos is not carried from one iteration to the next in the third case (see strange behavior of grep with global match [resolved]).

While each alternative has the same overhead, this overhead obscures the difference between the code under test. I imagine that in some cases the setup overhead might be so great that the code under test makes a negligible difference. So, I would like to perform setup of each iteration but have the time that this setup takes excluded from the benchmark calculation. I don't see any way to do this with Benchmark and don't know any other modules/tools that might support this.


In reply to Initializing iterations while benchmarking by ig

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.