Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

(dkubb) Re: (3) Returning the lowest key in a hash (or highest)

by dkubb (Deacon)
on Mar 25, 2001 at 07:22 UTC ( [id://66945]=note: print w/replies, xml ) Need Help??


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
    A lot of that is 'and' being a lot faster than ?:, rather than the multiple assigns.

    Update:  Wrong!  jcwren challenged me to benchmark it and almost all the time is in the assigns.

    p
      Cool!

      Update: darn

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://66945]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-28 18:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found