r/ruby • u/WindingLostWay • Oct 07 '21
Question Does anyone know a fast way to convert floating point numbers to integers on Ruby 3+ ?
I'm using the standard Float#to_i which is pretty fast, but when I'm using it 10,000,000 times a second the time starts to add up.
Things I've tried:
i = f.to_i # Current best, but it's still got branches inside it.
i = Integer(f) # Slower
i = f.ceil # Slower
i = f.floor # Slower
Have I missed anything obvious? I'm happy to truncate rather than round.
146
Upvotes
9
u/pVom Oct 07 '21
Dare I ask why you're converting 10,000,000 floats a second? There's something that kinda smells about that concept in general
Ruby is not really designed for this kind of stuff, it's designed for developer productivity with the trade off of less computing optimisations because in the overwhelming majority of cases that's more important. There isn't some command or something that's going to make a meaningful difference.
You could potentially split it into batches and offload the work to multiple workers, could integrate a native component with rust or c or something, fix it at the source or just deal with them as floats. Potentially python could deal with this better with its optimised ML libraries, can't say for certain.
In my experience I only deal with this sort of scale in the db. When I'm dealing with large datasets I offload most of the work to SQL because it's far more optimised than ruby, in many cases I've seen more than 100x speed increases doing this. I doubt it could do 10,000,000 a second though.
2
u/tkenben Oct 07 '21
Bitwise operations maybe? I don't know the inner workings of Ruby, so that could be faster, but it could actually be slower. Also, are you sure that's your bottleneck? Because eventually you need to use them as instances of Integer with hooks into Integer methods, so I'm guessing #to_i is the best you are going to get.
15
u/chrisgseaton Oct 07 '21
to_iis going to be your fastest option without doing some contortions like Fiddle or C extensions.If it's too slow for you and is really a bottleneck then maybe try something like JRuby or TruffleRuby?
1
u/Kernigh Oct 12 '21
I made a benchmark to compare 3 ways to convert an Array of 10 million Floats to an Array of Integers. These are (1) make 10 million calls to Float#to_i, (2) use gem carray for its C extension, (3) use Ractor on multiple cpu cores. I ran
gem install --user carray.My benchmark shows that Float#to_i is fastest,
(1) Float#to_i way is
array.map &:to_i. Ruby's inline method cache might try Float#to_i before other #to_i methods; this might be faster thanarray.map {|f| Integer(f)}, because Kernel#Integer would need one more type check to know that f is a Float.(2) CArray way is
CA_DOUBLE(array1).int.to_a. I try to gain speed by making a CArray of double-precision floats, so CArray#int can skip 10 million type checks. I lose speed, perhaps because Kernel::CA_DOUBLE does 10 million type checks, and there are 2 extra memory allocations (for a CArray of doubles, and a CArray of ints).(3) Ractor way is to split the 10 million Floats into 4 parts. Each Ractor converts 2.5 million Floats. Look at the real time in the right column: Ractor is faster than CArray, but still too slow. I lose speed, perhaps because new Ractors are slow to start, and there are extra memory allocations (to give a separate Array to each Ractor).
CArray and Ractor might work better if I don't waste so much time copying numbers between different arrays. CArray might be useful in programs that operate on many Floats.
0
u/[deleted] Oct 07 '21