simonm has asked for the wisdom of the Perl Monks concerning the following question:
The problem is that the standard Benchmark module doesn't isolate its test cases from one another. This means that the order that cases are run in can influence the results because side effects, either obvious or obscure, can accumulate and affect later tests.
Data in global variables is an obvious source of side effects; in the below example, the grep takes longer as more items are pushed onto the array, so the test functions that run later will be reported by Benchmark as being slower:
cmpthese( 1000, { "test_1" => sub { push @global, scalar grep 1, @global }, "test_2" => sub { push @global, scalar grep 1, @global }, "test_3" => sub { push @global, scalar grep 1, @global }, } );
To address this, I created a module that overrides the normal behavior of Benchmark to run each piece of code to be timed in a separate forked process. Just use Benchmark::Forking and the above benchmark reports the "correct" conclusion that the three tests run at approximately the same speed.
Feedback would be very welcome.
package Benchmark::Forking; use strict; use Benchmark; use vars qw( $VERSION $Enabled $RunLoop ); BEGIN { $VERSION = 0.9; $Enabled = 1; $RunLoop = \&Benchmark::runloop; } sub enable { $Enabled = 1 } sub disable { $Enabled = 0 } sub enabled { $#_ ? $Enabled = $_[1] : $Enabled } sub import { enable(); goto &Benchmark::import } sub unimport { disable() } sub Benchmark::runloop { $Enabled or return &$RunLoop; open( FORK, '-|') or print join "\n", @{ &$RunLoop } and exit; my @td = <FORK>; close( FORK ) or die $!; bless \@td, 'Benchmark'; }; 1;
Update: I removed the old POD from this post, but it's still available here. An updated version is now on CPAN.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Forking Benchmarks?
by tachyon (Chancellor) on Sep 04, 2004 at 10:26 UTC | |
|
Re: Forking Benchmarks?
by Rhys (Pilgrim) on Sep 04, 2004 at 10:37 UTC | |
by Aristotle (Chancellor) on Sep 04, 2004 at 15:50 UTC | |
by Rhys (Pilgrim) on Sep 04, 2004 at 17:35 UTC | |
|
Re: Forking Benchmarks?
by graff (Chancellor) on Sep 04, 2004 at 16:09 UTC | |
by simonm (Vicar) on Sep 04, 2004 at 17:07 UTC | |
|
Re: Forking Benchmarks?
by pbeckingham (Parson) on Sep 04, 2004 at 17:06 UTC | |
by Aristotle (Chancellor) on Sep 04, 2004 at 18:54 UTC | |
by simonm (Vicar) on Sep 04, 2004 at 17:08 UTC | |
|
Re: Forking Benchmarks?
by qq (Hermit) on Sep 06, 2004 at 00:13 UTC |