On Wed, Jan 19, 2011 at 3:55 AM, dblock <dblockdotorg / gmail.com> wrote:
> I am trying to improve performance of Euclidian distance between two
> hash maps. The code is pretty straightforward:
>
> def self.euclidian_distance(lhs, rhs)
>   
>     ݩ >  
>    
>    >  
>  > end
>
> Any suggestions?

Use Hash's default value mechanism or use "|| 0" and kick out
#has_key? calls.  I'd also rather use a proper Matrix or Vector class
instead of a Hash.  NArray also seems like a good and fast option.

Btw, why are you using an instance variable here?  That does not seem
to make any sense.

Also, it seems that your calculation is flawed because you calculate
the distance for keys which are present in both Hashes twice.  I'd
rather

require 'set'

def euclidian_distance(lhs, rhs)
  s = 0

  (lhs.keys.to_set.merge(rhs.keys)).each do |k|
    s += ( (rhs[k] || 0) - (lhs[k] || 0) ) ** 2
  end

  s
end


Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/