From: "John W. Kennedy" Date: 2004-01-13T12:11:38+09:00 Subject: Re: faster integer arithmetics & arbitrary precision floating number Dave Thomas wrote: > On Jan 12, 2004, at 15:43, Charles Mills wrote: >> What abouts Rubys design would make integer arithmetic slower than >> integer >> arithmetic in Perl? > class Fixnum > def +(other) > self.to_s + other.to_s > end > end > puts 1 + 2 #=> "12" > Ruby has to do full OO method dispatch for basic arithmetic operators. It also has to Fixnum.new the result. (And eventually, it has to GC every intermediate result.) x = (a + b) * c does something VERY approximately along the lines of: t = Fixnum.new (a + b) x = Fixnum.new (t * c) t = nil This is a basic tradeoff of having pure OO, and the reason that Java doesn't use pure OO, and one of the reasons that C++ doesn't. I notice, though, that the problem here is something of a worst case. With smaller numbers that don't involve Bignum, the Ruby times are better, about 1/4 to 1/3 the speed of Perl. And with larger numbers that are Bignum all the way through, they're also faster, though not as fast as the Fixnum case. So a large part of the problem is that Bignum is invoked before 0x7FFFFFFF. I suppose there is a reason that it is -- I can guess, but I don't know. For what it's worth, of the languages that I have with big-integer support, my perfect-number generator is fastest in GCLISP, next fastest in Ruby, and slowest in Java. (Note that that is a measurement of big-integer speed _only_.) -- John W. Kennedy "But now is a new thing which is very old-- that the rich make themselves richer and not poorer, which is the true Gospel, for the poor's sake." -- Charles Williams. "Judgement at Chelmsford"