From: Daniel Carrera Date: 2004-01-13T00:22:03+09:00 Subject: Re: faster integer arithmetics & arbitrary precision floating number One idea would be to make a "FastInt" class that stores inegers as regular C int's. It could work like this: int1 = FastInt.new(1073741823) int2 = FastInt.new(073741824) 1000000.times{ int1 + int2 } So addition would be done in C, which would be faster. Notice: If you create the integers about as often as you add them, the conversion from Ruby to C and back would more than compensate for any gains from C (I suspect). I would expec that this: 1000000.times do int1 = FastInt.new(1073741823) int2 = FastInt.new(073741824) int1 + int2 end would be even slower. Cheers, Daniel. On Tue, Jan 13, 2004 at 12:09:53AM +0900, David Garamond wrote: > 1. Is there a way in Ruby to speed up 32bit integer arithmetics (only > involving numbers & sums up to 2^32-1)? I want to use Ruby for > summarizing network traffic logs, but it's pathetically slow compared to > Perl: > > $ time ruby -e'1000000.times{1073741823+1073741824}' > > real 0m23.693s > user 0m5.720s > sys 0m0.610s > $ time perl -e'for(1..1000000){1073741823+1073741824}' > > real 0m1.142s > user 0m0.320s > sys 0m0.050s > > since 2**30 is already in the Bignum range. > > 2. Doing arbitrary integer math is already very convenient in Ruby > because of its automatic conversion. But Ruby still doesn't do seamless > conversion to arbitrary floating point numbers: > > $ irb > irb(main):001:0> 0.00000000000000001 > => 1.0e-17 > irb(main):002:0> 0.000000000000000001 > => 0.0 > > Any chance Ruby will do this in the future? Or perhaps in the nearer > future, include an arbitrary floating number package in its distribution > (is there any? GMP is GPL so it potentially a problem license-wise). > > -- > dave > -- Daniel Carrera | No trees were harmed in the generation of this e-mail. PhD student. | A significant number of electrons were, however, severely Math Dept. UMD | inconvenienced.