During my last presentation, someone raised a question about Java performance comparing to other languages like C#.NET and C++. Java's reputation of slowness seemed to precede itself for the last decade. I've met many who complain about Java being slow, when they have never actually written any Java code! This misunderstanding has also been amplified by many Java non-believers in the industry. I think it would be a good idea to do a little roundup on Java performance benchmarking and overview of HotSpot VM as it contribute to faster Java for future reference.
Java vs C/C++
David Agastine's classic works on Java Benchmarking
- http://blogs.sun.com/dagastine/entry/java_is_faster_than_c
- http://blogs.sun.com/dagastine/entry/sun_java_is_faster_than
Performance of Java versus C++ by J.P.Lewis and Ulrich Neumann
Java vs C#
Another of David's blog entry
Java vs Python and Ruby
C++ vs Java vs Python vs Ruby : a first impression
Hard Fight : Java vs. Python vs. Ruby
- This blog post is originally written in Thai, but the testing code and result are in English. So to see the code and performance summary, scroll down to the bottom of the page. A couple points to note about this test:
- The performance is subject to the code running. The author
does not claim to be an expert in Java, Python, Ruby. - As you can see along this post, the testing code has been revised many times. After each revision, the result changes. So, this benchmarking result subjects heavily to the quality of the testing code.
- The performance is subject to the code running. The author
There are many other benchmarking done and published on the web. Yet some of them are old and the information they provide is based on older version of JDK, so I will not include them here. Let me know if there are other good benchmarks out there. Several key factors contributing to Java's outstanding performance:
Default Sun JVM since Java 1.3. Apart from providing shared Java Runtime library and executing bytecode, HotSpot implements several optimization techniques that yield higher performance.
Just-in-Time(JIT) compilation:
The basic idea is that bytecode compilation is done selectively by JIT compiler when it's about to be executed. JIT compiler is also smart enough to cache heavily used code
(the hot spots!) in memory at runtime to reduce number of recompilation and reinterpretation. Another beautiful thing about JIT compiler is that the longer the application is running, it collects and analyzes more data. JIT runtime can then use this information optimize compilation for better
performance.
Adaptive optimization:
HotSpot adaptive optimizer can base compilation on current execution profile, making tradeoff between JIT compiling and interpreting instructions. The optimizer can collect data and schedule compilation accordingly to improve performance for current use case. So, in
theory, Java application running in optimized run-time environment can, if it is not already, be faster than C/C++.
Dynamic recompilation:
At a lower level, VM can recompile
some part of a program during execution time. Once the VM spots a repeating code pattern or redundancy in code that is running, it can tailor generated code to optimize current run-time environment. Adaptive optimization usually employs Dynamic recompilation as a part of its strategy.
JDK improvement
Java language compiler, along with HotSpot VM, is improving on every release. Every releases include bug fixed, code review, and additional optimization techniques. I think the JDK improvement on future releases will even be more significant after Sun open sourced JDK.
I think it is worth to think about reasons why many people still stuck with the idea that Java is slow, after all, HotSpot run-time optimization isn’t a new thing
- They had bad experience with earlier version of Java - 1.2 and earlier ones that didn't have JIT compiler in it.
- HotSpot optimization units improve performance over time as it collects more information from run-time environment. So the application can be sluggish on the first run.


