From: kero@... Date: 2005-10-31T07:08:15+09:00 Subject: Re: Time for built-in Rational and Complex classes? > > > I don't see why not. But using GCD as an example the C version would > > > have to handle fixnums, bignums and arbitrary types that respond to > > > +,-,*,/,etc. You could just do a straight translation into C using > > > rb_funcall(), but if you wanted to see a speed up for the common case > > > (fixnum and bignum) than it would require handling those cases specially > > > (using arithmetic in C and the rb_big_*() functions). > > > Is there a more straight forward solution? > > > > I can provide some C implementations of gcd for Fixnums. > > > > But first, is there anyone who has profiled Rational and can tell that gcd > > really is a major time consumer ? What do commonly used denominators look > > like and how do they affect the speed of gcd ? > > I've no idea of the details, but a major performance boost for > RubyGems came from using Time instead of Date. The profile showed > Integer#gcd as a key time sink, in the context of Date usage (i.e. > basic usage, not complex manipulation). > > I agree with people's comments that implementing GCD and LCM in C > would be a good start. From using Date, I found the same. for other applications, the snippet below may not be a speed gain. YMMV. I use a rule of thumb that fewer method calls is faster in Ruby. I made this and it was faster for me (and then forgot to post it somewhere) class Integer # Code without optimizations as in lib/rational.rb # this is faster for Date :) def gcd(other) a, b = self.abs, other.abs while b != 0 a, b = b, a % b end a end end HtH, Kero. PS: I hope I get this onto this ML now. I haven't subscribed for nothing ;) +--- Kero ------------------------- kero@chello@nl ---+ | all the meaningless and empty words I spoke | | Promises -- The Cranberries | +--- M38c --- http://members.chello.nl/k.vangelder ---+