Sign in to PrePAN

Benchmark::Parametric Measure code performance by passing an iteration count argument



#!/usr/bin/env perl

# Compare different ways of converting hash to boolean

use strict;
use warnings;
use Benchmark::Parametric;

my $bm = Benchmark::Parametric->new (
    teardown => sub { shift == $_ or die "Not ++ed enough"; },

# just a big hash
my %hash = ( 1 .. 20_000 );

print $bm->compare (
    noop   => sub { my $i; %hash             and $i++ for 1 .. $_; $i },
    keys   => sub { my $i; scalar keys %hash and $i++ for 1 .. $_; $i },
    exclam => sub { my $i; !!%hash           and $i++ for 1 .. $_; $i },
    scalar => sub { my $i; scalar %hash      and $i++ for 1 .. $_; $i },


Instead of iterating the same subroutine over and over again, this module passes an iteration counter to code under test, thus allowing to time precisely the iterated snippet.

It also allows to prepare complex test data and/or check the validity of results by providing "setup" and "teardown" functions.


This looks good to me, but I don't understand what benefit I would get over You say a couple things that seem different, but the code for using Benchmark looks the same. Could you add a section to the documentation describing what Benchmark::Parametric does that doesn't and providing an example of both side-by-side for comparison?

Please sign up to post a review.