ig has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Initializing iterations while benchmarking
by BrowserUk (Patriarch) on Aug 07, 2009 at 10:16 UTC | |
by ig (Vicar) on Aug 07, 2009 at 10:34 UTC | |
|
Re: Initializing iterations while benchmarking
by alexm (Chaplain) on Aug 07, 2009 at 10:15 UTC | |
by ig (Vicar) on Aug 07, 2009 at 10:20 UTC | |
by alexm (Chaplain) on Aug 07, 2009 at 10:33 UTC | |
by ig (Vicar) on Aug 07, 2009 at 10:43 UTC | |
by alexm (Chaplain) on Aug 07, 2009 at 11:05 UTC | |
| |
|
Re: Initializing iterations while benchmarking
by DStaal (Chaplain) on Aug 07, 2009 at 13:42 UTC |