FAQ
This patch is so that cmpthese() can be used in Apache::Benchmark. I
don't think the change in result codes will inconvenience anybody
because I've never seen/heard of cmpthese()'s results being used. The
original result code design was a mistake on my part.

Should apply against perl-current from this morning (patchlevel.h says
DEVEL12916).

A funny thing happened on the way to write a test suite, though;
lib/Benchmark.t looks to be a command-line development tool in addition
to a test suite. Please advise...

Thanks,

Barrie

--- perl-current/lib/Benchmark.pm Fri Jun 29 00:43:01 2001
+++ perl-current-benchmark/lib/Benchmark.pm Mon Nov 12 11:01:16 2001
@@ -196,17 +196,48 @@

=item cmpthese ( COUT, CODEHASHREF, [ STYLE ] )

-=item cmpthese ( RESULTSHASHREF )
+=item cmpthese ( RESULTSHASHREF, [ STYLE ] )

-Optionally calls timethese(), then outputs comparison chart. This
-chart is sorted from slowest to fastest, and shows the percent
-speed difference between each pair of tests. Can also be passed
-the data structure that timethese() returns:
+Optionally calls timethese(), then outputs comparison chart. This:

- $results = timethese( .... );
+ cmpthese( -1, { a => "++\$i", b => "\$i *= 2" } ) ;
+
+outputs a chart like:
+
+ Rate b a
+ b 2831802/s -- -61%
+ a 7208959/s 155% --
+
+This chart is sorted from slowest to fastest, and shows the percent speed
+difference between each pair of tests.
+
+c<cmpthese> can also be passed the data structure that timethese() returns:
+
+ $results = timethese( -1, { a => "++\$i", b => "\$i *= 2" } ) ;
cmpthese( $results );

-Returns the data structure returned by timethese() (or passed in).
+in case you want to see both sets of results.
+
+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( -1, { a => '++$i', b => '$i *= 2' }, "none" );
+
+returns a data structure like:
+
+ [
+ [ '', 'Rate', 'b', 'a' ],
+ [ 'b', '2885232/s', '--', '-59%' ],
+ [ 'a', '7099126/s', '146%', '--' ],
+ ]
+
+B<NOTE>: This result value differs from previous versions, which returned
+the C<timethese()> result structure. If you want that, just use the two
+statement C<timethese>...C<cmpthese> idiom shown above.
+
+Incidently, note the variance in the result values between the two examples;
+this is typical of benchmarking. If this were a real benchmark, you would
+probably want to run a lot more iterations.

=item countit(TIME, CODE)

@@ -661,10 +692,9 @@
}

sub cmpthese{
- my $results = ref $_[0] ? $_[0] : timethese( @_ );
+ my ($results, $style) = ref $_[0] ? @_ : ( timethese( @_[0,1] ), $_[2] ) ;

- return $results
- if defined $_[2] && $_[2] eq 'none';
+ $style = "" unless defined $style;

# Flatten in to an array of arrays with the name as the first field
my @vals = map{ [ $_, @{$results->{$_}} ] } keys %$results;
@@ -760,6 +790,8 @@
push @rows, \@row;
}

+ return \@rows if $style eq "none";
+
# Equalize column widths in the chart as much as possible without
# exceeding 80 characters. This does not use or affect cols 0 or 1.
my @sorted_width_refs =
@@ -791,7 +823,7 @@
printf $format, @$_;
}

- return $results;
+ return \@rows ;
}

Search Discussions

  • Jarkko Hietaniemi at Nov 12, 2001 at 4:19 pm

    On Mon, Nov 12, 2001 at 11:07:36AM -0500, Barrie Slaymaker wrote:
    This patch is so that cmpthese() can be used in Apache::Benchmark. I
    don't think the change in result codes will inconvenience anybody
    because I've never seen/heard of cmpthese()'s results being used. The
    original result code design was a mistake on my part.

    Should apply against perl-current from this morning (patchlevel.h says
    DEVEL12916).

    A funny thing happened on the way to write a test suite, though;
    lib/Benchmark.t looks to be a command-line development tool in addition
    to a test suite. Please advise...
    Ooops. My bad. It seems to be originally dprof.t (which is now missing).
    Victims of the great test renaming. Will rename it correctly.
    Thanks,

    Barrie

    --- perl-current/lib/Benchmark.pm Fri Jun 29 00:43:01 2001
    +++ perl-current-benchmark/lib/Benchmark.pm Mon Nov 12 11:01:16 2001
    @@ -196,17 +196,48 @@

    =item cmpthese ( COUT, CODEHASHREF, [ STYLE ] )

    -=item cmpthese ( RESULTSHASHREF )
    +=item cmpthese ( RESULTSHASHREF, [ STYLE ] )

    -Optionally calls timethese(), then outputs comparison chart. This
    -chart is sorted from slowest to fastest, and shows the percent
    -speed difference between each pair of tests. Can also be passed
    -the data structure that timethese() returns:
    +Optionally calls timethese(), then outputs comparison chart. This:

    - $results = timethese( .... );
    + cmpthese( -1, { a => "++\$i", b => "\$i *= 2" } ) ;
    +
    +outputs a chart like:
    +
    + Rate b a
    + b 2831802/s -- -61%
    + a 7208959/s 155% --
    +
    +This chart is sorted from slowest to fastest, and shows the percent speed
    +difference between each pair of tests.
    +
    +c<cmpthese> can also be passed the data structure that timethese() returns:
    +
    + $results = timethese( -1, { a => "++\$i", b => "\$i *= 2" } ) ;
    cmpthese( $results );

    -Returns the data structure returned by timethese() (or passed in).
    +in case you want to see both sets of results.
    +
    +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( -1, { a => '++$i', b => '$i *= 2' }, "none" );
    +
    +returns a data structure like:
    +
    + [
    + [ '', 'Rate', 'b', 'a' ],
    + [ 'b', '2885232/s', '--', '-59%' ],
    + [ 'a', '7099126/s', '146%', '--' ],
    + ]
    +
    +B<NOTE>: This result value differs from previous versions, which returned
    +the C<timethese()> result structure. If you want that, just use the two
    +statement C<timethese>...C<cmpthese> idiom shown above.
    +
    +Incidently, note the variance in the result values between the two examples;
    +this is typical of benchmarking. If this were a real benchmark, you would
    +probably want to run a lot more iterations.

    =item countit(TIME, CODE)

    @@ -661,10 +692,9 @@
    }

    sub cmpthese{
    - my $results = ref $_[0] ? $_[0] : timethese( @_ );
    + my ($results, $style) = ref $_[0] ? @_ : ( timethese( @_[0,1] ), $_[2] ) ;

    - return $results
    - if defined $_[2] && $_[2] eq 'none';
    + $style = "" unless defined $style;

    # Flatten in to an array of arrays with the name as the first field
    my @vals = map{ [ $_, @{$results->{$_}} ] } keys %$results;
    @@ -760,6 +790,8 @@
    push @rows, \@row;
    }

    + return \@rows if $style eq "none";
    +
    # Equalize column widths in the chart as much as possible without
    # exceeding 80 characters. This does not use or affect cols 0 or 1.
    my @sorted_width_refs =
    @@ -791,7 +823,7 @@
    printf $format, @$_;
    }

    - return $results;
    + return \@rows ;
    }
    --
    $jhi++; # http://www.iki.fi/jhi/
    # There is this special biologist word we use for 'stable'.
    # It is 'dead'. -- Jack Cohen

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupperl5-porters @
categoriesperl
postedNov 12, '01 at 4:14p
activeNov 12, '01 at 4:19p
posts2
users2
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase