in reply to Getting Max Value from Different Hashes

My guess is that the biggest gain you could make is by not storing four values in four different hashes. The overhead of having references to four tiny hashes just dwarves whatever you win by selecting the appropriate way of finding the maximum of four values.
  • Comment on Re: Getting Max Value from Different Hashes

Replies are listed 'Best First'.
Re^2: Getting Max Value from Different Hashes
by Anonymous Monk on Aug 01, 2005 at 15:47 UTC
    Don't guess! Benchmark!
    use List::Util; use Benchmark 'cmpthese'; our($h0, $h1, $h2, $h3); our @vals = map {int rand 100} 1 .. 4; $$h0{key0} = $vals[0]; $$h1{key1} = $vals[1]; $$h2{key2} = $vals[2]; $$h3{key3} = $vals[3]; our($m1, $m2, $m3, $m4, $m5, $m6); cmpthese -1 => { h1 => '$m1 = List::Util::max values %$h0, values %$h1, values %$h2, values %$h3', h2 => '$m2 = List::Util::max map {values %$_} $h0, $h1, $h2, $h3' +, h3 => '$m3 = (sort values %$h0, values %$h1, values %$h2, values %$h3)[-1]', h4 => '$m4 = (sort map {values %$_} $h0, $h1, $h2, $h3)[-1]', l1 => '$m5 = List::Util::max @vals', l2 => '$m6 = (sort @vals)[-1]', }; __END__ Rate h4 h2 h3 h1 l2 l1 h4 160777/s -- -37% -46% -66% -68% -89% h2 255999/s 59% -- -13% -46% -49% -83% h3 295080/s 84% 15% -- -38% -42% -80% h1 477204/s 197% 86% 62% -- -6% -68% l2 506415/s 215% 98% 72% 6% -- -66% l1 1483834/s 823% 480% 403% 211% 193% --
    Your guess it right though. Even the fastest solution using hashes (in combination with List::Util::max) is slower than sorting the list and taking the last element.