adimas - Fotolia
Is Java slow? Compared to C++, it's faster than you think
If you find that Java is slower than other languages, such as C++, here's how to better compare the two and the major differences between compiled and interpreted languages.
Is Java slow?
It's a commonly asked question in a world where every software developer wants to get the most out of every clock cycle their programs consume. It was also a topic at a Java "Birds of a Feather" session at Oracle Code One 2019.
The consensus is that no, it is not. In fact, when compared against its peers, Java is pretty fast. Java is able to compete with -- and sometimes outperform -- other interpreted languages based on how it manages memory, completes just-in-time (JIT) compiles and takes advantage of various features of its underlying architecture.
Compiled vs. interpreted languages
The fact remains that Java is an interpreted language. In contrast, C++ -- a language which Java is often compared to -- is statically typed. It is the dynamic nature of the Java language that often gets users worried about possible speed issues.
With an interpreted language, code is compiled into an intermediary form before it is converted into machine code. In Java, this intermediary is called bytecode. At runtime, a Java virtual machine (JVM) turns this bytecode into machine code, and it's the machine code that runs on the processor. Java bytecode can run on any computer with a JVM installed, which is the basis of Java's write-once, run-anywhere mantra. But, this intermediary translation from bytecode to machine code consumes resources. This step irks those who are primarily focused on performance.
Java vs. C++ performance
In contrast, a program written in C++ gets compiled directly into machine code -- without an intermediary translation required at runtime. This is one reason why C++ programs tend to perform faster than those written in Java.
Gil TeneCTO, Azul
However, there are exceptions. When a C++ program is compiled, it's optimized based on the architecture of the system on which it runs. For example, if a C++ program is targeted against an ARM-based processor, it can take advantage of all the features that architecture currently offers. It cannot take advantage of any features that don't yet exist in that architecture.
When a Java program runs, it inspects the underlying system, and the JVM can optimize the machine code it creates based on the available features. If a new processor comes out with new optimization features, the JVM can immediately generate machine code to use those features. In contrast, a program written in C++ would need to be completely recompiled and redeployed to use the new features.
So, is Java slow?
"Java will work faster than C on the CPU you don't have yet," Azul CTO Gil Tene said at the Code One session. "When the AVX-512 vector support came out for Skylake CPUs, JIT compilers that knew how to vectorize ran twice as fast, while C stayed at the same speed," Tene said. Until those old C programs were recompiled, the Java programs maintained a performance edge.
Many factors influence an application's speed.
Is Java slow? No.
Is Java a program as fast as one written in C++? Not always, but there are cases where it can, in fact, be faster.