Java vs C++ Performance Comparison, JIT Compilers, Java Hotspot & C++ Native Compiler
The gap between Java vs C++ performance has been narrowing for the past years; studies (Y Nikishkov, G Nikishkov & Savchenko, 2003; Taboada et al. 2013; Lewis & Neumann 2013; Oracle n.d.; Oancea et al. 2011) have shown that Java can achieve similar performance to natively compiled languages such as C++. Taboada et al. (2013, p425) argue that thanks to the Just-in-Time (JIT) compiler of the Java Virtual Machine (JVM) Java obtains native performance from Java bytecode.
Just-In- Time (JIT) compiler can significantly speed up the execution of Java applications. The JIT, which is an integral part of the JVM takes the bytecodes and compile them into native code before execution. Since Java is a dynamic language, the JIT compiles methods on a method-by-method basis just before they are called. If the same method called many times or if the method contains loop with many repetitions the effect of re-execution of the native code can dramatically change the performance of Java code (Y Nikishkov & G Nikishkov & Savchenko 2003. p2402).
With JIT the compilation can be optimized to the targeted CPU and the operating system model where the application runs. The system is able to collect statistics about how the program is actually running in the environment it is in, and it can rearrange and recompile for optimum performance.
A further development of a JIT compiler is made by Oracle with the Java Hotspot Compiler. They argue that the main reasons JIT optimization are less effective than C++ are:
1) Java is dynamically safe; it ensures programs do not violate the semantics or allow directly access to untrusted memory. Dynamic type test must be frequently performed.
2) The Java language allocates all objects on the heap, in contrast to C++, where many objects are stack allocated. This means that object allocation rates are much higher for the Java language than for C++.
3) In the Java language, most method invocations are virtual, and are more frequently used than in C++.
4) Java technology-based programs can change on the fly due to the ability to perform dynamic loading of classes.
Oracle argues that the Java Hotspot virtual machine overcome these issues by running the program using an interpreter, and analyze the code as it runs to detect hot spots in the program. Then it focuses the attention of a global native code optimizer on the hotspot. This allows avoiding compilation of infrequently used code. This hot spot monitoring is continued dynamically as the program runs, so that it literally adapts its performance on the fly to the user’s needs.
Oracle indicates that the main compiler optimizations are: deep inlining and inlining of potentially virtual calls, fast instanceof/checkcast, range check elimination, loop unrolling, feedback-directed optimizations – the Java virtual machine profiles the program execution in the interpreter before compiling the Java bytecode to optimized machine code and the profiling information is used later by the compiler to more aggressively and optimistically optimize the code in certain situations. This allows to Java applications to run in at similar or grater speed than C++ programs.
A study (Oancea et al., 2011) has shown that the use of Java with Hotspot technologies when developing complex data applications can achieve performances comparable to natively compiled languages. Furthermore, a recent study (Haubl & Wimmer & Mossenbock 2013) has shown an increased peak of performance in relation to current JIT with extra optimization to the Java virtual machine.
Nikishkov, Y, Nikishkov, G & Savchenko, V 2003 ‘Comparison of C and Java performance in finite element computations’, Computers and Structures, vol. 81, no. 24, pp. 2401-2408.
Taboada, GL, Ramos, S, Exposito, RR, Tourino, J & Doallo, R 2013 ‘Java in the High Performance Computing arena: Research, practice and experience’, Science of Computer Programming, vol. 78, no. 5, pp. 425.
Lewis, J.P, Neumann, U, 2004 ‘Performance of Java versus C++’, University of Southern California, <http://scribblethink.org/Computer/javaCbenchmark.html>
Oracle n.d., ‘The Java HotSpot Performance Engine Architecture’ <http://www.oracle.com/technetwork/java/whitepaper-135217.html>
Oancea, B, Rosca, IG, Andrei, T & Iacob, AI 2011 ‘Evaluating Java performance for linear algebra numerical computations’, Procedia Computer Science, vol. 3, pp. 474-478.