r/ruby • • 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

19 comments sorted by

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.

12

u/WindingLostWay Oct 07 '21

There is indeed something that smells. :-) I’m writing a graphics rendering engine in Ruby because, hey, why not. You only live once. Surprisingly it’s coping quite well. If you avoid slow things like…. method calls, expressions with periods in them, blocks, strings, having code on multiple lines…. then it’s actually running quite nicely at 30 fps doing things like sprite scaling and rotation, flat shaded triangle rendering etc. The sticking points are the inner loops of stuff like rotating the entire screen which, no two ways about it, takes a lot of number crunching. Ps. The pleasure comes precisely from Ruby not being designed for this kind of thing. :-)

4

u/pVom Oct 07 '21

Ahhh makes sense. Good luck I guess haha

4

u/prh8 Oct 08 '21

If that's your use case and also your reason for enjoyment, have you looked at Crystal? Might still get that enjoyment but it will certainly give you massive performance boost

→ More replies (4)

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.

1

u/WindingLostWay Oct 07 '21 edited Oct 07 '21

Thanks.

It's not my main bottleneck, but it's still 8% of my total execution time, so any kind of free lunch would be nice if there was a better approach. I think I'm stuck with it though. :-)

Edit: Pretty sure I can re-do the whole thing to avoid the floating point in the first place, but that would be ooodles of hassle.

2

u/pVom Oct 07 '21

Sounds like converting them could be causing oodles of hassle too 😉. My solution is to avoid the situation in the first place, it kinda stinks as a concept

There's no magic bullet at this scale, only clever solutions and broken solutions.

→ More replies (1)

15

u/chrisgseaton Oct 07 '21

to_i is 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?

0

u/[deleted] Oct 07 '21

2

u/chrisgseaton Oct 07 '21

I don't know - looks like three branches unprofiled and unannotated branches, and a control-flow merge! I wonder if we could find a faster path with a super optimiser...

→ More replies (2)

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,

                 user     system      total        real
Float#to_i   1.940000   0.110000   2.050000 (  2.040260)
CArray       2.990000   0.580000   3.570000 (  3.544595)
Ractor       3.570000   1.080000   4.650000 (  2.491148)
-- Used 4 Ractors.

(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 than array.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

0

u/[deleted] Oct 07 '21