From: dblock Date: 2011-01-20T01:25:27+09:00 Subject: Re: Improving performance of hash math > 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. - has_key has almost no cost, made no difference - my data is sparse, there're a lot of zero values - using an array forces you to do N calculations (N is the max width of the array), an Array was 3x slower, especially with a fast growing data set (haven't tried NArray) > Btw, why are you using an instance variable here?  That does not seem > to make any sense. - fixed > 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 - re-read the core, you're wrong, the second half only adds values that are not present in the first array > 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 - this is 3x slower because of the cost of to_set.merge (the code is nicer though :)