views:

555

answers:

5

What are the most easy mistakes to make that can be performance sinks on Android?

The documentation mentions "some floating point operations" can be "on the order of milliseconds" - has someone tested this?

For the sake of discussion, let's assume its running on a G1/similar device.

+1  A: 

For rendering-specific performance tips, see these Google I/O 2009 talks:

Ates Goral
A: 

The reason they want you to avoid using float is because it's rarely implemented on phone cpu's(Probbly arm), and has to be implemented in software which is slow. Fixed point is usally surported in hardware although.

Some phones do implement floating point in hardware, but since you don't what phone your app might be deployed to they don't risk it.

A lot of people also say avoid using objects, back in the days of really slow phones and java me people used to write java procedurally with static functions. I would not recommend that now though.

UK-AL
A: 

Don't worry about performance until you know the code has major issues. The little tweaks (ints instead of floats, using iterators instead of explict array enumeration) tend to be really minor and you are better off looking at them when your app is down. Make the 5% of the code that is slow more complex rather than making the whole app.

hacken
While I agree about not optimizing early etc, this isnt the issue here. If for instance all float operations take 1ms and all int operations 1ns, designing all data structures for fixed point from the beginning is just common sense.
Viktor
If you have a 50ms budget to do the work, if it takes 1ms or 1ns doesn't matter. Get the code working and then worry about the details. Converting between fixed point and floats is a day or two of work at best.Get you app running with efficient algorithms and then if you need to do these optimizations.In almost every app I have looked at, the big performance wins are never in micro optimizations. They are in making sure you are not doing stupid things (drawing objects 2x, sorting when it isn't needed,...).There are cases when microoptimization is right. You will know it when it happens
hacken
+1  A: 

According to the Android folks, you should avoid using the Interface names for collections. For instance, the standard best practice for standard Java would be to say

List<String> strings = new ArrayList<String>();

Whereas in Android they say it's better to declare it with its runtime type

ArrayList<String> strings = new ArrayList<String>();
I82Much
note that -- despite being in the documentation -- this isn't actually true. i've corrected this in the documentation for froyo. (in particular, the froyo version of the documentation won't contain any claim that isn't substantiated by a benchmark.)
Elliott Hughes
+1  A: 

wrt floating point:

on a G1, adding two floats takes about 400ns. adding two ints takes about 250ns.

on a nexus one running eclair (pre-JIT), both operations take around 120ns. (ints are slightly faster, but you'd have to be microbenchmarking to notice.) there's a small percentage difference between int and long, and float and double, but basically if you can afford one, you can probably afford the other.

other current devices will be somewhere between these extremes. (other operations will differ too. multiplication is more expensive than addition/subtraction, and division is more expensive still. no current devices have hardware integer division.)

but don't obsess about any of this until you have a problem. chances are, your performance problems will be down to a poor choice of algorithm or data structure, just like everyone's performance problems always are.

most of the current (eclair) performance documentation is incorrect. benchmark things yourself, on the device(s) you care about.


but if you were really asking "what should a desktop/server java programmer watch out for?", i'd suggest: unnecessary allocation. you don't have a spare core to do your GC like you do on the desktop/server, and you don't have gigabytes of heap like you do on the desktop/server. if you're doing GC, you're not doing real work, and your heap will be at most 24MiB on current devices. so you might want to avoid unnecessary allocation in inner loops.

Elliott Hughes