Skip to content Skip to sidebar Skip to footer

How Android Runtime Compiles Java More Efficiently Than The Clang C/с++ Compiler (android Ndk)?

I was absolutely sure that C\C++ native code will run faster than Java code. And it is. My simple C/C++ benchmark (random arithmetic operations on int array) runs 5-7 times faster

Solution 1:

How Android Runtime compile more efficient native code than CLang (Android NDK) C/C++ compiler?

The JIT compiler complements ART's current ahead-of-time (AOT) compiler and improves runtime performance.

Although JIT and AOT use the same compiler with a similar set of optimizations, the generated code might not be identical. JIT makes use of runtime type information can do better inlining and makes on stack replacement (OSR) compilation possible, all of which generate slightly different code.

JIT compilation activities

Is there any performance advantage in writing native C/C++ code on recent Android OS versions?

Definitely yes for old Android devices. Old Android devices use Dalvik VM that interprets code. Java code will be 5-7 times slower than the same C/C++ code on Dalvik VM. But on ART, in most cases, Android Runtime profile-guided compilation generates much more efficient native code based on application execution statistics, but Java still ~30-35% slower than optimized C/C++ code on Android 8.1 and ~10-20% faster on Android 11.

https://source.android.com/devices/tech/dalvik/jit-compiler

Conclusion: In my humble opinion, there is no performance advantage in writing C/C++ code for recent Android devices (starting from v7.0 Nougat - ART VM), unless you're an experienced C/C++ developer and deeply understand CPU architecture, so you can do optimization much better than Android Runtime does.

Also, Android NDK (C/C++) is still the only way for Android Developers to:

  1. Port your native C/C++ code to Android.
  2. Use C/C++ game engines and libraries (like Vulkan or TensorFlow).
  3. Use platform-specific APIs not available in Android SDK.

Post a Comment for "How Android Runtime Compiles Java More Efficiently Than The Clang C/с++ Compiler (android Ndk)?"