Skip to content

Commit

Permalink
Simplify timing method lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
kentfredric committed Sep 17, 2014
1 parent fc52de9 commit 4050698
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions lib/Benchmark/CSV.pm
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ sub scale_values {
my $nargs = ( my ( $self, $value ) = @_ );
if ( $nargs >= 2 ) {
croak 'Cant set scale_values after finalization' if $self->{finalized};
$self->{scale_values} = $value;
return ( $self->{scale_values} = $value );
}
return $self->{scale_values} if exists $self->{scale_values};
return ( $self->{scale_values} = undef );
Expand Down Expand Up @@ -127,14 +127,35 @@ my $timing_methods = {
},
};

=for Pod::Coverage timing_method
=cut

sub timing_method {
my $nargs = ( my ( $self, $method ) = @_ );
if ( $nargs >= 2 ) {
croak 'Cant add instances after execution/finalization' if $self->{finalized};
if ( not exists $timing_methods->{$method} ) {
croak "No such timing method $method";
}
return ( $self->{timing_method} = $method );
}
return $self->{timing_method} if $self->{timing_method};
return ( $self->{timing_method} = 'hires_wall' );
}

sub _timing_method {
my ($self) = @_;
return $timing_methods->{ $self->timing_method };
}

sub _compile_timer {
## no critic (Variables::ProhibitUnusedVarsStricter)
my ( $self, $name, $code, $sample_size ) = @_;
## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars);
my $run_one = q[ $code->(); ];
my $run_batch = join qq[\n], map { $run_one } 1 .. $sample_size;
$self->{timing_method} ||= 'hires_wall';
my ( $starter, $stopper, $diff ) = map { $timing_methods->{ $self->{timing_method} }->{$_} } qw( start stop diff );
my ( $starter, $stopper, $diff ) = map { $self->_timing_method->{$_} } qw( start stop diff );
my $sub;
if ( $self->per_second and $self->scale_values ) {
$diff = "( ( $diff > 0 ) ? (( 1 / $diff ) * $sample_size ) : 0 )";
Expand Down

0 comments on commit 4050698

Please sign in to comment.