Benchmarks - The comparison benchmarker
use Benchmarks sub { my $x = 2; +{ times => sub { $x * $x * $x * $x }, raise => sub { $x ** 4 }, }; };
Benchmarks is the Benchamark wrapper for comparing routines easily.
When this module was loaded, all you need to do is pass the CODE reference that returns the hash reference.
use Benchmarks sub { +{ 'routine_name_1' => sub { '... some code ...' }, 'routine_name_2' => sub { '... some code ...' }, }; };
Then the comparison will be invoked and show the result like below.
Benchmark: running raise, times for at least 1 CPU seconds... raise: -1 wallclock secs ( 1.07 usr + 0.00 sys = 1.07 CPU) @ 8895180.37/s (n=9517843) times: 2 wallclock secs ( 1.10 usr + 0.00 sys = 1.10 CPU) @ 4051316.36/s (n=4456448) Rate times raise times 4051316/s -- -54% raise 8895180/s 120% --
Benchmarks can show a pure benchmark(NOT to compare) like below.
use Benchmarks sub { my $x = 2; sub { $x * $x * $x * $x }; # only one code };
then the result like this.
timethis for 1: 1 wallclock secs ( 1.07 usr + 0.00 sys = 1.07 CPU) @ 4164904.67/s (n=4456448)
Benchmarks exports *ALL* functions from Benchmark. You can use Benchmarks module as same as Benchmark module.
Benchmarks
Benchmark
use Benchmarks; warn timestr( countit(1, sub { bless +{}, 'Foo' } ) );
More information about functions: https://metacpan.org/pod/Benchmark#Standard-Exports and https://metacpan.org/pod/Benchmark#Optional-Exports
When you use Benchmarks, you can throw several args like below.
use Benchmarks sub { # benchmark hash or code. }, COUNT, STYLE, TITLE;
example STYLE:
use Benchmarks sub { my $x = 2; +{ times => sub { $x * $x }, raise => sub { $x ** 2 }, }; }, 100, "none";
example STYLE and TITLE:
use Benchmarks sub { my $x = 2; sub { $x * $x }; }, 100, 'all', '2 times';
You can pass(return) these args at runtime.
use Benchmarks sub { my $x = 2; sub { $x * $x }, 100, 'all', '2 times'; };
This module includes the penchmarks command. It's generator of the Benchmarks template.
$ penchmarks GENERATE_BENCHMARK_FILE_PATH
Then you can edit the GENERATE_BENCHMARK_FILE_PATH file and run it.
GENERATE_BENCHMARK_FILE_PATH
Benchmarks is hosted on github http://github.com/bayashi/Benchmarks
Welcome your patches and issues :D
Dai Okabayashi <bayashi@cpan.org>
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.
To install Benchmarks, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Benchmarks
CPAN shell
perl -MCPAN -e shell install Benchmarks
For more information on module installation, please visit the detailed CPAN module installation guide.