Android NDK Performance

android.gifAndroid apps aren’t just implemented using Java. You can also compile and run c/c++ (NDK) code. This obviously runs faster but how much faster? With newer Dalvik implementations over time, how much faster is c/c++ now?

Learn OpenGL ES has a useful post that compares the speed of a digital signal processing (DSP) filter implemented in Java and c. It shows, in this instance, that Java runs 17.78 times slower than c. However, what’s more interesting is that it’s possible to manually modify the Java code to do things, such as inlining function calls, to get the Java to only 2.87 the speed of c.

The problem with such Java optimisation is that it makes the code significantly less readable. However, I can see the case/opportunity for a Proguard style tool that optimises specifically for speed.

Note that the NDK isn’t just used for improved performance. I have also used it for…

  • Portability – To allow compiling of large open source and particularly proprietary libraries of code not available in Java
  • Increased Memory – To allow access to more memory than is available on the Java heap for example to manipulate whole large images that can’t be loaded into Java
  • Deeper Device Access – Access to particular OEM APIs, for example hardware jpg encoding/decoding, not available from Java