@android Best Practice

android development

Performance Tips

There are two basic rules for writing efficient code:

  • Don’t do work that you don’t need to do.
  • Don’t allocate memory if you can avoid it.

Avoid Creating Unnecessary Objects

Prefer Static Over Virtual

Use Static Final For Constants

Avoid Internal Getters/Setters

Use Enhanced For Loop Syntax

Consider Package Instead of Private Access with Private Inner Classes

Avoid Using Floating-Point

As a rule of thumb, floating-point is about 2x slower than integer on Android-powered devices.

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.

Also, even for integers, some processors have hardware multiply but lack hardware divide. In such cases, integer division and modulus operations are performed in software — something to think about if you’re designing a hash table or doing lots of math.

Know and Use the Libraries

Use Native Methods Carefully

Performance Myths

Before you start optimizing, make sure you have a problem that you need to solve. Make sure you can accurately measure your existing performance, or you won’t be able to measure the benefit of the alternatives you try.

You may also find Traceview useful for profiling, but it’s important to realize that it currently disables the JIT, which may cause it to misattribute time to code that the JIT may be able to win back. It’s especially important after making changes suggested by Traceview data to ensure that the resulting code actually runs faster when run without Traceview.