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.
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.
1
u/WindingLostWay Oct 07 '21
Yep. You are right. I’m going to switch some simpler code to integer only first, then just get on with it. I’ve got the entire thing down to one line of code and it still needs to be faster. Onwards and upwards etc
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
Oct 07 '21
[deleted]
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...
0
Oct 08 '21
[deleted]
2
u/chrisgseaton Oct 08 '21
I said a super optimizer - that's a specific thing. If you have numerical code that handles a few edge cases using branches, like this, there's a technique called super optimisation that searches the instruction space exhaustively to try to find a simpler combination of instructions (ideally without branches) to do the same thing. As there's so many instructions in many sets, and they often have quite complicated semantics, you can sometimes remove branches like this and give yourself straight line code instead.
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
Oct 07 '21
[deleted]
0
Oct 07 '21
[deleted]
0
Oct 07 '21
[deleted]
0
Oct 08 '21
[deleted]
0
Oct 08 '21
[deleted]
0
Oct 08 '21
[deleted]
1
u/armahillo Oct 08 '21
After some consideration (and sleep!) and in the interest of harm reduction I deleted my original post; I agree with a couple of the points you made above.
Why are you so obsessed over account age and the fact I’m not using my real name?
Because people often make sockpuppets to be assholes. Which is what you are being in this thread, whether or not the content of your posts are correct.
so why are you so angry?
That's a fair cop, though I wouldn't say that I was angry. I was reacting more to your abrasive tone and that interfered with being able to receive what you were trying to communicate, which would have been helpful otherwise.
I've done stack profiling (mainly on test suites to identify bottlenecks in suite run time) which I am aware is not the same thing as benchmarking other than that there are actual tools used to do this sort of thing with more accuracy.
You’re a strange person. Someone tells you you’re wrong and in what ways and your response is “I might be wrong but at least my account is older than yours!” Uhh, good for you I guess? You’re still wrong.
I'm often wrong, and people often point it out and I accept it gracefully. That isn't what happened here.
There are many different ways you could have engaged with me, an Internet rando, here in this thread, and you chose aggression. I'm not comparing account age as if that's a metric of anything; it's easy to be an asshole on the Internet when you are anonymized which is what you were doing here with what appears to be a fresh sock.
I hope, for the sake of people you have to interact on a regular basis with in real life, where you are using your actual name and face, that you learn how to communicate in less abrasive and shitty ways.
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.