Android apps are interpreted rather than compiled. Does this make them slower than iOS apps at runtime?
Answer
Java isn't interpreted on Android. Android apps are compiled to bytecode by the developer. Bytecode is a compact representation of the program: smaller than the source code written by the programmer, but still not directly executable by the CPU. Some optimizations, such as dead code removal, can be made at this stage.
When you load the app on a device, the Dalvik JVM compiles the bytecode to native executable code, just as it's about to run. This is just-in-time compilation. It causes a brief slow-down while the program waits to be compiled, but after that there's no performance overhead, because the code has been compiled to native executable code.
There are some performance advantages to doing it this way instead of compiling up-front on the developer's computer. The app can be compiled for the particular CPU on the phone, taking advantage of its hardware features and using its performance characteristics. For example, it can use hardware floating-point operations if the CPU supports it. In addition, a clever JIT compiler (admittedly, Dalvik is not quite this clever) can monitor the way the program runs, and perform optimizations based on the way the program is used in real use. It might recompile the code with better branch hinting once it has seen which options are turned on and off in your environment, on your phone. An up-front compiler doesn't have this information to use.
Dalvik uses the Dalvik cache and other techniques to mitigate the drawbacks of JIT compilation. The new JVM for Android L and later, ART, replaces the JIT entirely with an ahead-of-time compiler. This compiles the bytecode to native executable code when the app is installed, to get most of the advantages of JIT without the delay loading the app.
Don't forget that Android apps don't entirely consist of Java. Developers have the NDK to write all or part of their apps in C or C++, for performance-critical parts of the app, especially for games. Special-purpose interfaces like OpenGL and Renderscript let programmers take advantage of special hardware like the GPU and SIMD coprocessor for some kinds of computation.
So really, there's no simple answer to your question. Using JIT instead of up-front compilation makes some things faster, some things slower. It's just one part of the overall performance of the OS.
No comments:
Post a Comment