3

I'm developing an Android game. At the moment I'm writing code that should calculate the best route between two objects, using the A* algorithm. Inside the algorithm i'm using doubles to calculate everything.

When running time consuming algorithms on my computer the time difference between calculating with Integers and Doubles is almost zero (maybe a few nanoseconds).

Question one:

Can this behavior also seen on Android phones? I know my computer has multiple ALU's one for integers on for floating points etc. But how about phones? Android phones can be very different when it comes down to hardware. I only have two test phones, but today there are hundreds of phones available on the market, which means that running tests on two of my phones would not give me reliable data, at least that's what I think.

Question two:

For my game it doesn't matter allot if I switch to Integers, the calculated route maybe less perfect but that doesn't bother me. So is it worth switching to integers? Or would switching to integers break the algorithm? Does anyone have experience with this?

Rolf ツ
  • 8,611
  • 6
  • 47
  • 72
  • 2
    Older Android phones do not have a floating-point co-processor (ARM A5 CPUs). On such devices, using `double` will be much, much slower than using `int`. In general, `double` will be slower than `int`, even on devices with floating-point co-processors. – CommonsWare Mar 01 '12 at 12:32

3 Answers3

2

You should check http://developer.android.com/guide/practices/design/performance.html

Luis
  • 1,294
  • 7
  • 9
  • 1
    Good point, normal seen i always look there, but not today. They say that floating point is two times slower than integer. Then they tell you that double or float does not matter and that you should prefer one of them? They tell something about integers and that dividing is done using software in some cases. Still not clear what to use, maybe i should make two versions and let users choose? – Rolf ツ Mar 01 '12 at 12:39
  • If you are a developer and cannot check which is better, the user is in an even worse position. It may depend on the phone. Probably create a version and test it in a low end phone. – Luis Mar 01 '12 at 13:18
2

As a general rule of thumb, integer arithmetic is faster than floating point arithmetic because it is simpler to implement. (With floating point, the hardware has to deal with both the mantissa and exponent parts, and (AFAIK) it cant do this in parallel.)

However, the performance ratio is going to vary from one hardware implementation to the next, and it is hard to make predictions that are any better than guessing. (For a start, it depends on marketing strategies and performance vs battery life tradeoffs.)


But to be honest, I'd be focusing more on which of integer and floating point is going to work best with your game's algorithms. If they require one and you use the other, then you are in for a lot of pain (unnecessary coding, testing, bug fixing, etc).

Or do you guys think switching to integers will break the algorithm?

It could do. It could also make them a lot more complicated. It all depends on the nature of the algorithms. Certainly, you are in for a lot of recoding and retesting.

Personally, I'd only contemplate doing this if the game was simply too slow even after I'd tried profiling and tuning the performance hotspots. And I'd only do it if I was prepared to take the risk that the re-coding effort might achieve no benefit ... or worse.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

Integer arithmetic has a few advantages:

  • It's usually faster. This depends on the hardware, but most hardware has more efficient integer operations. Also some hardware lacks FPU capabilities and actually has to emulate floating point instructions which is much slower.
  • You don't have to worry about rounding issues. Floating point inaccuracy can cause some subtle bugs and issues.
  • It's simpler - you don't have to worry about special cases like NaN and Infinity, and it's easier to display / format numbers.

So in general, if you don't strictly need floating point numbers then you should stick with integers in your algorithms.

To answer your specific questions:

  1. You've got no guarantee regarding hardware support on Android phones. On modern phones FPU support is likely, but you can't rely on it. So use integers if you can.
  2. A* pathfinding works fine with integers, assuming a grid-like map data structure and positive integer movement costs. So you should be able to switch to integers just fine, and it will probably will be faster. Note that you may need to scale up movement costs appropriately, so instead of a movement cost of 1.0 per square you might make it 100 for example. But apart from that the algorithm should be the same.
Community
  • 1
  • 1
mikera
  • 105,238
  • 25
  • 256
  • 415