Bench: a simpler benchmark module

There was a post in 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 Sample output:

$ perl -Ilib -MBench -MMoose -e1

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
bench { subs => { a => sub { ... }, ... } }
instead of just
bench { a => sub { ... }, ... }
or, if you're often using dummy names like "a", and "b", just
bench [sub { ... }, ...]

Why so many blank lines? It makes it difficult to read.

Leave a comment

About Steven Haryanto

user-pic A programmer (mostly Perl 5 nowadays).