BlackBerry Arithmetic Performance

Playing with some animation for a new BlackBerry product (see next post) and wondering about performance of double/float/Fixed32 arithmetic I came across this post on Blurry Words, which is exactly what I was looking for.

Basically performance depends on the type of arithmetical operation you’re doing (though double is always slowest, usually by a wide margin). For addition and subtraction, Fixed32 is faster, for multiplication and division float wins out. Some sample results:

BlackBerry Bold

Variable Type Add/Sub (ms) Mult (ms) Div (ms)
Fixed32 153 597 776
float 317 242 550
double 514 366 1931
long 114 117 225
int 96 90 114
short 102 102 125
byte 104 107 127

The net.rim.device.api.math.Fixed32 class is interesting – I’ve mostly used it in scaling EncodedImages. It packs a fixed decimal representation of a number into a 32 bit int datatype, using the leftmost 16 bits for the whole part, and the rightmost 16 bits for the fractional part. This has the result that the normal + and – operators work as expected, but multiply and divide are handled through special functions.

Though I’ve gotta disagree with the poster’s conclusions – Fixed32 is not strictly legacy, it’s only been available since OS 4.0, at which time float and double were available on BlackBerry.

There’s a lot more interesting discussion and more results for different devices just in this post, so I encourage to read the original!

Leave a Reply

Your email address will not be published. Required fields are marked *