Even less hassle when benchmarking Perl code

About a year ago I blogged about Bench, a simpler alternative to Benchmark. I got tired of having to type this on the terminal:

% perl -MBenchmark -e'timethis($count, "code")'
% perl -MBenchmark -e'timethese($count, {label1 => sub { code1 }, label2 => sub { code2 })'

I dislike the interface because it forces me to remember/decide/do too many things. I don't want to have to remember whether to use timethis()/timethese(). I don't want to have to pick the appropriate $count. I don't want to have to label the subs. With Bench I can just type:

% perl -MBench -e'bench sub { code }'
% perl -MBench -e'bench [sub { code1 }, sub { code2 }]'

Guess what, I got tired of having to type the above too :-) So now with the latest release of Bench, a command-line script bench is provided. You can now just type:

% bench 'code'
Benchmarking sub { code } ...
100 calls (15427/s), 0.0065s (0.0648ms/call)

% bench 'code1' 'code2'
Benchmarking sub { code1 }, sub { code2 } ...
a: 100 calls (15368/s), 0.0065s (0.0651ms/call)
b: 100 calls (13335/s), 0.0075s (0.0750ms/call)

bench is also nice enough to detect filename in the first argument, and run that instead.

% bench prog.pl [args ...]; # a Perl script, run via do()
Benchmarking /abs/path/to/prog.pl ...
0.123s

1 Comment

I've should have known this before wrinting my dumbbench articles

Leave a comment

About Steven Haryanto

user-pic A programmer (mostly Perl 5 nowadays). My CPAN ID: SHARYANTO. I'm sedusedan on perlmonks. My twitter is stevenharyanto (but I don't tweet much). Follow me on github: sharyanto.