The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Bench - Benchmark running times of Perl code

VERSION

version 0.09

SYNOPSIS

 # time the whole program
 % perl -MBench -e'...'
 0.0123s

 # basic usage of bench()
 % perl -MBench -e'bench sub { ... }'
 100 calls (58548/s), 0.0017s (0.0171ms/call)

 # get bench result in a variable
 % perl -MBench -E'my $res = bench sub { ... }'

 # specify bench options
 % perl -MBench -E'bench sub { ... }, 100'
 % perl -MBench -E'bench sub { ... }, {n=>-5}'
 304347 calls (60665/s), 5.017s (0.0165ms/call)

 # use Dumbbench as the backend
 % perl -MDumbbench -MBench -E'bench sub { ... }'
 % perl -MBench -E'bench sub { ... }, {dumbbench=>1, dumbbench_options=>{...}}'
 Ran 26 iterations (6 outliers).
 Rounded run time per iteration: 2.9029e-02 +/- 4.8e-05 (0.2%)

 # bench multiple codes
 % perl -MBench -E'bench {a=>sub{...}, b=>sub{...}}, {n=>-2}'
 % perl -MBench -E'bench [sub{...}, sub{...}]'; # automatically named a, b, ...
 b: 100 calls (5357/s), 0.0187s (0.1870ms/call)
 a: 100 calls (12120/s), 0.0083s (0.0825ms/call)
 Fastest is a (2.267x b)

DESCRIPTION

This module is an alternative to Benchmark. It provides some nice defaults and a simpler interface. There is only one function, bench(), and it is exported by default. If bench() is never called, the whole program will be timed.

This module can utilize Dumbbench as the backend.

FUNCTIONS

bench SUB(S)[, OPTS] => RESULT

Run Perl code and time it. Exported by default. Will print the result if called in void context. SUB can be a coderef for specifying a single sub, or hashref/arrayref for specifying multiple subs.

Options are specified in hashref OPTS. Available options:

  • n => INT

    Run the code n times, or if negative, until at least n seconds.

    If unspecified, the default behaviour is to run at most 1 second or 100 times.

  • dumbbench => BOOL

    If 0, do not use Dumbbench even if it is available. If 1, require and use Dumbbench. If left undef, will use Dumbbench if it is already loaded.

  • dumbbench_options => HASHREF

    Options that will be passed to Dumbbench constructor, e.g. {target_rel_precision=>0.005, initial_runs=>20}.

SEE ALSO

bench, command-line interface for this module

Benchmark

Dumbbench

AUTHOR

Steven Haryanto <stevenharyanto@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Steven Haryanto.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.