Int Faster Than Float?

Nov 28, 2010 at 5:17am

Int Faster Than Float?

I’m writing a patch that controls real-time hardware, and thus I want the patch to run as fast as possible. The patch pulls up presets (via pattrstorage) that each contain 32 numbers, then runs some mathematical operations on each of the 32 numbers, then sends control data out to a series of motor controllers.

I have the option in this patch to sacrifice precision slightly by using integers throughout instead of floats. But I’m wondering if there’s any point to doing this. Will a patch run faster if you use Int instead of Float? If so, how much faster?

#53593
Nov 28, 2010 at 7:09am

cant tell you how much faster, but a float is 25% longer … there is a difference for sure.

btw, it is a pity that there are no SHORTs in max.

#192704
Nov 28, 2010 at 7:31am

Hello aheadbehind,

you can have an idea with this kind of benchmark ;

– Pasted Max Patch, click to expand. –

IMHO :

the difference is really not significant enough to use INTS instead of FLOATS, if you need FLOATS.

It’s a good habit to optimize things after “profiling”.

Time is relative ; for example in previous benchmark if you do process one after the other, time for them are equal on my computer :

– Pasted Max Patch, click to expand. –

so find the bottleneck of your patch before optimizations ;-)

#192705
Nov 28, 2010 at 7:45am

For the math part of your patch, floating point is more expensive, but barely.

– Pasted Max Patch, click to expand. –
#192706
Nov 28, 2010 at 7:56am

Hello Chris Muir,

sorry : i edit my post while yours ;-)

time is no the same too if you are high/low priority thread ….

– Pasted Max Patch, click to expand. –

Anyway as Chris Muid said : barely.

#192707
Nov 28, 2010 at 7:01pm

All,

Thanks for the quick response. This answers the question perfectly! My machine yields a 2% tax for floats. Not bad considering the increased accuracy I’ll get for my application.

I’ll run floats to start, then I can always experiment with int later if I need to do some speed optimization.

Thanks Again!!

#192708
Nov 29, 2010 at 12:16am

Not bad considering the increased accuracy

Well it really depends what you call accuracy ;-) I would just use ints, when it’s appropriate and float when it’s appropriate. Appropriate would be based on the actual data type that you needs to process, and not the efficiency that you expect to gain. It’s unlikely that your bottleneck will be there.

#192709
Nov 29, 2010 at 7:28pm

I would reiterate EJ’s “depends on what you call accuracy.” Floats present a lot of surprises to the unwary. Search the forums on “floating point precision” and you will turn up loads of threads documenting the potential confusion.

In many ways ints are lot easier to comprehend: addition, subtraction, and multiplication work the way you learned in school (as long as the results are can be written with less than 10 digits); once you get used to the idea that the division of two ints always yields an int (possibly losing a fractional remainder), you’re good to go.

Floats, OTOH, don’t handle many simple fractions the way you learned in school. Take one, divide it by nine, then multiply by nine and see what you get. And never mind the Infs and NaNs and denormalized and negative zeros. No, floats are not “more accurate”.

And, although it is true that the processor can handle ints a few cycles faster than a float, every object and every patch cord in your patcher is using dozens if not hundreds of cycles more than your basic arithmetic operations. If you really need to worry about float vs. int, Max/MSP isn’t the right tool to be worrying with.

In your case, it’s more likely that Max/MSP is the right tool and int vs. float is the wrong worry.

Just to underscore the point about the cost of patch cords, here’s another example of Chris’ patch doing the exact same floating point calculation. I think you’ll find it’s a little faster than anything you’ve seen in this thread so far.

– Pasted Max Patch, click to expand. –
#192710

You must be logged in to reply to this topic.