Bench: a simpler benchmark module
There was a post in blogs.perl.org or Planet Perl Iron Man (sorry, forgot the exact article) that said something along the line of: "Benchmark is a fine module, but for simplicity I'll use the time command". Which immediately hit home with me, because I too very seldomly use Benchmark. I guess the problem is I almost always have to perldoc it before using it, and there are quite some extra characters to type.
So last weekend I wrote Bench (repo) that's hopefully simpler enough to get used more.
To benchmark your program, just type: perl -MBench yourscript.pl
. Sample output:
$ perl -Ilib -MBench -MMoose -e1 0.229s
Bench exports a single function, bench(), by default. To time a single sub, use: perl -MBench -e'bench sub { ... }'
. By default it will call your sub at most 100 times or 1 second. Here's a sample output:
258411 calls (129165/s), 2.0006s (0.0000s/call)
To benchmark several subs: perl -MBench -e'bench {a=>sub{...}, b=>sub{...}}'
or perl -MBench -e'bench [sub{...}, sub{...}]'
. Sample output:
a: 100 calls (12120/s), 0.0083s (0.0825ms/call) b: 100 calls (5357/s), 0.0187s (0.187ms/call)
Bench will automatically use Dumbbench if it's already loaded, e.g.: perl -MDumbbench -MBench -e'...'
. Or you can force Bench to use Dumbbench: perl -MBench -e'bench sub { ... }, {dumbbench=>1}'
.
That's about it currently.
This looks useful, but I have one tiny nit: Why is it
instead of justbench { subs => { a => sub { ... }, ... } }
or, if you're often using dummy names like "a", and "b", justbench { a => sub { ... }, ... }
?bench [sub { ... }, ...]
Why so many blank lines? It makes it difficult to read.
@Mick: which blank lines are we talking about here? I checked this page using Firefox & Chrome(aside from Opera which I normally use) and there are no extra lines.
Or are you referring to my cross-post here? http://id-perl.blogspot.com/2011/03/bench-simpler-benchmark-module.html . Yeah, I usually copy paste directly. Apparently MT's and Blogger's rendering rules are different.
@educated_foo: makes sense. I was lumping the subs inside options hashref, but I think your syntax is nicer. Will be included in v0.03. Thanks!