From: Dominik Bathon Date: 2006-02-19T03:41:36+09:00 Subject: Re: patch for rational.rb (gcd) On Sat, 18 Feb 2006 18:42:03 +0100, Mathieu Bouchard wrote: > On Sun, 19 Feb 2006, Dominik Bathon wrote: > >> $ ruby gcd_bench.rb >> user system total real >> gcd 0.390000 0.000000 0.390000 ( 0.390554) >> gcd2 6.410000 0.030000 6.440000 ( 6.469019) >> gcd3 0.170000 0.000000 0.170000 ( 0.168139) >> gcd4 0.120000 0.000000 0.120000 ( 0.122690) > > What's differing in versions of Ruby so that the difference in execution > times between gcd2 and gcd3 is either nothing or huge? ruby-1.9's mathn.rb does already have the euclid gcd in mathn.rb: class Integer remove_method(:gcd2) def gcd2(other) min = self.abs max = other.abs min, max = max % min, min while min > 0 max end while 1.8.4's mathn.rb doesn't: class Integer def gcd2(int) a = self.abs b = int.abs a, b = b, a if a < b pd_a = a.prime_division pd_b = b.prime_division gcd = 1 for pair in pd_a as = pd_b.assoc(pair[0]) if as gcd *= as[0] ** [as[1], pair[1]].min end end return gcd end So the new implementation obviously has already been merged, just not in 1.8. Dominik