in reply to Re: Returning the lowest key in a hash (or highest)
in thread Returning the lowest key in a hash (or highest)
I have one more optimization for you MeowChow. Inside your _min() and _max() subroutines, if the test returns false, it reassigns the original value back to itself. You can get around a 25% speed increase in the algorithm if you only assign the $_ value to $max or $min if it passes the test.
Here's a short benchmark that illustrates the difference better:
#!/usr/bin/perl -w use Algorithm::Numerical::Shuffle qw(shuffle); use Benchmark qw(cmpthese); #Randomize the array contents my @array = shuffle 0..1000; cmpthese(-3, { max_dkubb1 => sub { max_dkubb1(@array) }, max_tilly => sub { max_tilly(@array) }, }); sub max_dkubb1 { my $max = shift; $max < $_ and $max = $_ for @_; return $max; } sub max_tilly { my $max = shift; $max = $max < $_ ? $_ : $max for @_; return $max; }
On my system the max_dkubb1() routine runs just over 25% faster than the other.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (dkubb) Re: (3) Returning the lowest key in a hash (or highest)
by petral (Curate) on Mar 27, 2001 at 06:46 UTC | |
by and (Pilgrim) on Mar 27, 2001 at 20:12 UTC |