Forums > MaxMSP

Int Faster Than Float?

November 28, 2010 | 5:17 am

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?


November 28, 2010 | 7:09 am

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.


November 28, 2010 | 7:31 am

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 ;-)


November 28, 2010 | 7:45 am

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

– Pasted Max Patch, click to expand. –

November 28, 2010 | 7:56 am

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.


November 28, 2010 | 7:01 pm

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!!


November 29, 2010 | 12:16 am

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.


November 29, 2010 | 7:28 pm

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. –

Viewing 8 posts - 1 through 8 (of 8 total)