3

A page on the Android developer's site mentions that you should (quote): "Avoid Using Floating-Point" (link: Performance Tips)

After reading the whole article, it seems that some of the items there may be a bit outdated (perhaps written a few years ago).

I would like to know whether this guideline is still valid today, or whether it is really bad (x2 performance hit, even on today's hardware) to use floating point types?

lysergic-acid
  • 19,570
  • 21
  • 109
  • 218
  • It's never been [prohibited](http://dictionary.reference.com/browse/prohibit). – user2864740 Oct 22 '13 at 18:30
  • 1
    A division or two (or a thousand) won't change anything: ok, it's 2x slower, but 2x slower than something very very fast. You will certainly feel the difference with math-intensive applications (game engines &co.), but until then, just code and profile. – Stefano Sanfilippo Oct 22 '13 at 18:30
  • I don't ever see advice to avoid floating-point math when unneeded being *invalid*. Integer math will just always be faster. Like Stefano says, though, you'll only notice it when you're doing a *lot* of it. – Geobits Oct 22 '13 at 18:39
  • @Geobits On today's beefy desktop CPUs, (most) floating point math is as fast as integer math, or even faster when vectorized (e.g. SSE2 includes float vector operations but not the integer equivalent). –  Oct 22 '13 at 19:12
  • @delnan True, I shouldn't say "always". But the question isn't about beefy desktop CPUs. It's about mobile devices, which cover a *huge* range of capabilities, some of which have little to no hardware support for FP math. Until the lower-end hardware goes away, it can definitely make a difference. – Geobits Oct 22 '13 at 19:15
  • @Geobits I know that perfectly well, I was just objecting to the "always" because it's a common misconception. But by the way, I wouldn't be surprised if mobile *G*PUs did float math as fast or faster than integer math -- graphics is float-heavy *everywhere*. It's not just a matter of silicion and power consumption but also one of priorities. –  Oct 22 '13 at 19:18

2 Answers2

4

Older Android devices like ARMv6 had no processor support for floating point arithmetic. If you stick to using Java your usage of floats would be accomplished through emulation and although it would work on older devices, your software would be extremely slow and power hungry if you were using floats in your display loop or something similar that updates 20+ times per second.

ARMv7 and beyond have floating point arithmetic support so it isn't such a big deal even though fixed point or integer arithmetic would be preferred if you thought your software would also have to support older devices.

See related question for OpenGL/high performance Android applications: Floating point or fixed-point for Android NDK OpenGL apps?

Community
  • 1
  • 1
nvuono
  • 3,323
  • 26
  • 27
2

IMHO, the statement to Avoid Using Floating-Point is ridiculous. If you need floating-point, you need floating-point. End of the line. You should not avoid it when you need it. Because, you will probably try to find ways around it with integers, but the calculations will be more complex and less clear for the programmer to read/debug code.

When you take a look at this gorgeous answer on SO, you will see that Floating Point Operations (FLOPS) are still really fast. FLOPS per cycle for sandy-bridge and haswell SSE2/AVX/AVX2. They speak about multiple FLOPS per cycle!

Community
  • 1
  • 1
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • 2
    "If you need floating-point, you need floating-point. End of the line." Once you have some experience, I'd say this is pretty much true. Beginners sometimes *think* they need floating point when they don't, though. Seconds vs milliseconds, kilometers vs meters, etc. Just keeping the advice in mind might help them see things on a different scale, even if it isn't gospel. – Geobits Oct 22 '13 at 18:58
  • 1
    @Martijn good answer, however mobile devices aren't running on haswell, i wonder how close the results are. – lysergic-acid Oct 22 '13 at 19:24
  • 1
    @lysergic-acid: Yes, I know. The answer is on more recent and more desktop specific hardware. There is one about ARM Cortex A-9 in the linked answer. I guess that comes close. – Martijn Courteaux Oct 22 '13 at 19:32