Open y opened 7 years ago
I'm not against this feature. If someone sends a suitable pull request I can add it.
I have a partial solution that processes the :float
option, but in a generic manner so future options can be easily added. The only part I couldn't figure out is how how to display the number as non-scientific in Benchmark::Dumb::_rate_str():
diff --git a/lib/Benchmark/Dumb.pm b/lib/Benchmark/Dumb.pm
index f380577..7ee18cc 100644
--- a/lib/Benchmark/Dumb.pm
+++ b/lib/Benchmark/Dumb.pm
@@ -24,11 +24,24 @@ our @EXPORT_OK = qw(
timediff timestr timesum
);
our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
+my %OPT;
# strip out :hireswallclock
sub import {
my $class = shift;
- my @args = grep $_ ne ':hireswallclock', @_;
+
+ # Removes any import that looks like a tag but isn't defined as such, and
+ # interprets it as an option.
+ my @args;
+ for my $arg (@_) {
+ if ($arg =~ /^:(.*)/ and !exists $EXPORT_TAGS{$1}) {
+ $OPT{$1} = 1;
+ }
+ else {
+ push @args, $arg;
+ }
+ }
+
$class->export_to_level(1, $class, @args);
}
@@ -47,7 +60,7 @@ sub _dumbbench_from_count {
return Dumbbench->new(
# TODO configurable default settings?
- %opt,
+ %OPT, %opt,
);
}
diff --git a/lib/Dumbbench.pm b/lib/Dumbbench.pm
index 1d80e2e..40ea399 100644
--- a/lib/Dumbbench.pm
+++ b/lib/Dumbbench.pm
@@ -23,7 +23,7 @@ use Class::XSAccessor {
outlier_rejection
subtract_dry_run
)],
- accessors => [qw(verbosity)],
+ accessors => [qw(verbosity float)],
};
@@ -242,10 +242,11 @@ sub report {
$options ||= {};
Carp::carp( "The second option to report was not a hash ref" )
unless ref $options eq ref {};
+ my $float = $options->{float} || $self->float;
foreach my $instance ($self->instances) {
my $result = $instance->result;
- my $result_str = ($options->{float}) ? unscientific_notation($result) : "$result";
+ my $result_str = $float ? unscientific_notation($result) : "$result";
if (not $raw) {
Can you make this a pull request?
Since most people use the Benchmark::Dumb interface, it would make sense to reimplement the float option as a constructor option to Dumbbench instead of as an option to report().