Benchmark::Dumb(3)
π Benchmark::Dumb(3) β the glow-up edition π
okay bestie, welcome to Benchmark::Dumb, the Benchmark.pm compatibility layer for Dumbbench. yes it's a real Perl manpage, no I will not be normal about it π«Ά
β¨ NAME β¨
Benchmark::Dumb β a Benchmark.pm compatibility layer for Dumbbench π
πΈ SYNOPSIS πΈ
use Benchmark::Dumb qw(:all);
cmpthese(
0.05, # 5% precision
{
fast => 'fast code',
slow => 'slow code',
}
);
# etc
literally that's it, that's the whole vibe β¨
π¦ DESCRIPTION π¦
so this module gives you an interface that's similar to the functional interface of the Benchmark module. except!! under the hood it's actually using the Dumbbench benchmarking tool. for various reasons, the interface and the output of the benchmark runs are not exactly the same. and among other reasons, you'd be losing out on some of Dumbbench's advantages if it tried to be a perfect clone. π
to understand this documentation you're gonna need some familiarity with how Benchmark.pm works, since mostly what we're doing here is explaining how this module is different. iykyk π
please read the next section carefully to understand the most important differences:
π Differences to Benchmark.pm π
this is a list of differences to the interface and behaviour of the Benchmark module. it may not be complete. if it's not, please let me know π
- the
$countparameter is interpreted very differently!!
with Benchmark.pm, specifying a positive integer meant that the benchmark should be run exactly $count times. a negative value indicated that the code should be run until $count seconds of cumulated run-time have elapsed.
with Benchmark::Dumb, we can do better π
a positive integer specifies the minimum number of iterations. Dumbbench may choose to run more iterations to arrive at the necessary precision.
specifying a certain target run-time (via a negative number for $count) may seem like a tempting idea, but if you care at all about the precision of your result, it's quite useless. this usage is not supported by Benchmark::Dumb!
instead, if you pass a positive floating point number as $count, the fractional part of the number willbe interpreted as the target relative precision that you expect from the result. (yes, "willbe" β that's the original typo, i'm keeping it, it's iconic π)
finally, supplying a 0 as $count means that Dumbbench will be invoked with the default settings. this is good enough for most cases π
- there are no exported functions by default!
- the
:hireswallclockoption is ignored. we always use the hi-res wallclock! and while we're on the topic: we also only use wallclock times. - the cache-related functions aren't implemented because we don't use a cache π«
- the original
Benchmark.pmimplementation provides a rudimentary object-oriented interface. we do not faithfully copy that. see "METHODS" below. - the benchmark code will be run in a special package. it will not be run in the caller package (at this time). if you need access to previously-set-up package variables, you will need to include a
packagestatement in your code. - the
debugmethod is not implemented and neither is thecountitfunction. - some things that were previously considered functions are now considered primarily methods (see "METHODS" below). but they are all importable and callable as functions π«Ά
π₯ FUNCTIONS π₯
these functions work mostly (see the $count gotcha above π) like the equivalent functions in the Benchmark module, but the textual output is different in that it contains estimates of the uncertainties. some of the style and format options of the original functions are ignored for the time being.
i'm quoting the Benchmark documentation liberally. π
β timethis(COUNT, CODE, [TITLE, [STYLE]])
time COUNT iterations of CODE. CODE may be a string to eval or a code reference. unlike with the original Benchmark, the code will not run in the caller's package. results will be printed to STDOUT as TITLE followed by the timestr(). TITLE defaults to timethis COUNT if none is provided.
STYLE determines the format of the output, as described for timestr() below.
please read the section on "Differences to Benchmark.pm" for a discussion of how the COUNT parameter is interpreted. π
returns a Benchmark::Dumb object.
β timeit(COUNT, CODE)
arguments: COUNT is the number of times to run the loop (see discussion above π), and CODE is the code to run. CODE may be either a code reference or a string to be eval'd. unlike with the original Benchmark, the code will not run in the caller's package.
returns a Benchmark::Dumb object.
β timethese(COUNT, CODEHASHREF, [STYLE])
the CODEHASHREF is a reference to a hash containing names as keys and either a string to eval or a code reference for each value. for each (KEY, VALUE) pair in the CODEHASHREF, this routine will call
timethis(COUNT, VALUE, KEY, STYLE)
the routines are called in string comparison order of KEY.
the COUNT must be positive or zero. see discussion above π
returns a hash reference of Benchmark::Dumb objects, keyed by name.
β cmpthese(COUNT, CODEHASHREF, [STYLE]) cmpthese(RESULTSHASHREF, [STYLE])
optionally calls timethese(), then outputs a comparison chart. this:
cmpthese( 500.01, { a => "++\$i", b => "\$i *= 2" } ) ;
outputs a chart like:
Rate/s Precision/s a b
a -3.59e+07 6e+06 -- -535.3%
b 8.24e+06 220000 -123.0% --
this chart is sorted from slowest to fastest, and shows the percent speed difference between each pair of tests as well as the uncertainties on the rates and the relative speed difference. the uncertainty on a speed difference may be omitted if it is below one tenth of a percent.
cmpthese can also be passed the data structure that timethese() returns:
my $results = timethese( 100.01, { a => "++\$i", b => "\$i *= 2" } ) ;
cmpthese( $results );
in case you want to see both sets of results π if the first argument is an unblessed hash reference, that is RESULTSHASHREF; otherwise that is COUNT.
returns a reference to an ARRAY of rows, each row is an ARRAY of cells from the above chart, including labels. this:
my $rows = cmpthese( 500.01, { a => '++$i', b => '$i *= 2' }, "none" );
returns a data structure like:
[
[ '', 'Rate/s', 'Precision/s', 'a', 'b' ],
[ 'a', '-4.43e+06', '120000', '--', '93+-6.9%' ],
[ 'b', '-2.294e+06', '52000', '-48.2%', '--' ]
];
π METHODS π
please note that while the original Benchmark objects practically asked for manual introspection since the API didn't provide convenient access to all information, that practice is frowned upon with Benchmark::Dumb objects. you have been warned π€ if there's a piece of API missing, let me know.
there's no public constructor for Benchmark::Dumb objects because it doesn't do what the Benchmark constructor did: it's not running time() for you.
π name()
returns the name of the benchmark result if any. (not in Benchmark.)
π iters()
returns the number of samples taken.
π timesum($other_benchmark)
returns a new Benchmark::Dumb object that represents the sum of this benchmark and $other_benchmark.
timesum($b1, $b2) was a function in the original Benchmark module and may be called as a function on two Benchmark::Dumb objects as well. it is available for importing into your namespace.
π timediff($other_benchmark)
returns a new Benchmark::Dumb object that represents the difference between this benchmark and $other_benchmark ($this-$other).
timediff($b1, $b2) was a function in the original Benchmark module and may be called as a function on two Benchmark::Dumb objects as well. it is available for importing into your namespace.
π timestr()
returns a textual representation of this benchmark.
timestr($b) was a function in the original Benchmark module and may be called as a function on a Benchmark::Dumb object as well. it is available for importing into your namespace.
π SEE ALSO π
- Dumbbench
- Benchmark
- some of the documentation was taken from the documentation for
Benchmark.pm's functions. π
πΈ AUTHOR πΈ
Steffen Mueller, <smueller@cpan.org>
π COPYRIGHT AND LICENSE π
Copyright (C) 2010, 2012 by Steffen Mueller
this library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.1 or, at your option, any later version of Perl 5 you may have available.
okay that's the whole page bestie π«Ά this was perl v5.42.0, dated 2025-07-25, and it's giving Benchmark::Dumb(3) main character energy. go benchmark something. periodt π β¨
π¦ where this page comes from
- fedora 43: perl-Dumbbench 0.505-3.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms