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?
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.
For the math part of your patch, floating point is more expensive, but barely.
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!!
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.
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.