From: Robert Klemme Date: 2011-01-19T18:13:32+09:00 Subject: Re: Improving performance of hash math On Wed, Jan 19, 2011 at 3:55 AM, dblock 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) >    @s = lhs.inject(0.0) do |s, (k,v)| >      rhs.has_key?(k) ? (s + (v - rhs[k])**2) : (s + v**2) >    end >    @s = rhs.inject(@s) do |s, (k,v)| >      lhs.has_key?(k) ? s : (s + v**2) >    end >    @s > 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/