3

I am using floats for some Android Java game graphics, but the Math library trig functions all return double, so I have to explicitly cast them.

I understand that floats are quicker to process than doubles, and I do not need high precision answers.

e.g. which is better:

screenXf = (float) (shipXf + offsetXf * Math.sin(headingf) - screenMinXf);

or

screenXf = shipXf + offsetXf * (float) (Math.sin(headingf)) - floatScreenMinXf;

I suppose other questions would be 'how can I test this on an emulator without other factors (e.g. PC services) confusing the issue?' and 'Is it going to be different on different hardware anyway?'

Oh dear, that's three questions. Life is never simple :-(

-Frink

FrinkTheBrave
  • 3,894
  • 10
  • 46
  • 55
  • Related: http://stackoverflow.com/questions/523531/fast-transcendent-trigonometric-functions-for-java – finnw Feb 04 '11 at 21:49
  • Related: http://stackoverflow.com/questions/2475494/why-are-there-so-many-floats-in-the-android-api/2479296#2479296 – Bert F Feb 04 '11 at 22:04

2 Answers2

1

This is definitely dependent on the HW. I know nothing about the target platforms, but on a current PC it takes the same amount of time while floats were about twice as fast as doubles on a i386.

Unless your emulator can report the cycle count, you can't find it out as the HW of your PC has little in common with the HW of the target platform. When the target platform were your PC, than I'd recommend http://code.google.com/p/caliper/ for this microbenchmark.

maaartinus
  • 44,714
  • 32
  • 161
  • 320
1

Consider using FloatMath.sin() instead.

FloatMath

Math routines similar to those found in Math. Performs computations on float values directly without incurring the overhead of conversions to and from double.


But note this blurb in the android docs:

http://developer.android.com/guide/practices/design/performance.html#avoidfloat

Designing for Performance
...
In speed terms, there's no difference between float and double on the more modern hardware. Space-wise, double is 2x larger. As with desktop machines, assuming space isn't an issue, you should prefer double to float.

Although this guy @fadden, purportedly one of the guys who wrote the VM, says:

Why are there so many floats in the Android API?

On devices without an FPU, the single-precision floating point ops are much faster than the double-precision equivalents. Because of this, the Android framework provides a FloatMath class that replicates some java.lang.Math functions, but with float arguments instead of double.

On recent Android devices with an FPU, the time required for single- and double-precision operations is about the same, and is significantly faster than the software implementation. (The "Designing for Performance" page was written for the G1, and needs to be updated to reflect various changes.)

His last sentence ("page ... need to be updated") refers to the page I referenced above, so I wonder if he is referring to that sentence about "no difference" that I quoted above.

Community
  • 1
  • 1
Bert F
  • 85,407
  • 12
  • 106
  • 123