From: Gary Wright Date: 2011-01-19T13:28:05+09:00 Subject: Re: Improving performance of hash math On Jan 18, 2011, at 9:55 PM, 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? > Switch to using #each with an explicit accumulator variable instead of #inject. Gary Wright