briandfoy / dumbbench

More reliable benchmarking without thinking
https://metacpan.org/pod/Dumbbench
Other
5 stars 4 forks source link

Make float option accessable from Benchmark::Dumb #2

Open y opened 7 years ago

y commented 7 years ago

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().

briandfoy commented 6 years ago

I'm not against this feature. If someone sends a suitable pull request I can add it.

y commented 6 years ago

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) {
briandfoy commented 6 years ago

Can you make this a pull request?