I would also add List::Moreutils, which has a minmax function using an improved algorithm to evaluate min and max at once: #!/usr/bin/perl
use strict;
use warnings;
use List::Util qw( min max reduce );
use List::MoreUtils qw( minmax );
use Benchmark qw( cmpthese );
my @array = map { rand 100 } 1 .. 1_000_000;
my $count = 0;
cmpthese -10, {
util_min_max => sub {
my $min = min(@array);
my $max = max(@array);
},
util_reduce => sub {
my ( $min, $max ) = @{ (reduce {
my $r= ref $a ? $a : [($a) x 2];
if ($b < $r->[0]) {
$r->[0]= $b;
} elsif ($b > $r->[1]) {
$r->[1]= $b;
}
$r
} @array) };
},
moreutils_minmax => sub {
my ( $min, $max ) = minmax(@array);
},
};
__END__
Rate util_reduce util_min_max moreutils_m
+inmax
util_reduce 0.846/s -- -92%
+ -96%
util_min_max 10.5/s 1146% --
+ -45%
moreutils_minmax 19.2/s 2169% 82%
+ --
I added the reduce method because I did not know if it would perform better, but the result does not surprise me given the bunch of function calls that are involved. Anyway, this is what benchmarks are for :)
Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf
Don't fool yourself.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|