I know there is an opinion that programs written in Java and running under JVM a
ID: 659097 • Letter: I
Question
I know there is an opinion that programs written in Java and running under JVM as as fast as C++ programs, after introducing just-in-time (JIT). I see many cases when Java is terribly slow for simple programs compared to C++, i.e. about factor 2-3 slower than equivalent C++ programs.
Just give you a small example: This is simple benchmark code to merge particles in clusters:
github.com/chekanov/hepjet
Go to "scjet_cpp" and type "make; make run". This runs C++ code. Then go to "scjet_java" and type "make; make run". This runs the same code written in Java.
Th first C++ code finishes merging particles within 50 msec (i7 desktop). Exactly the same Java code finishes for 170 ms. There is nothing special about the code, i.e. identical implementation in C++ and Java.
Any comment?
Explanation / Answer
Your measurement is probably flawed.
If you're just running both applications and measure the time from start to end in the terminal, you are not measuring execution time, but also startup time. For Java application, this means that those 170 ms. also contain JIT compilation.
Moreover, 50 ms. vs. 170 ms. is not representative. Doing a task in a loop and obtaining 50 s. vs. 170 s. will be more relevant.
You can't just take a single project and say: "This language is better, because this particular project executes faster with this language." A single project is not representative enough. Maybe authors knew C++ more than Java. Maybe they missed opportunities to optimize code.
Compilers you use may make a huge difference. Some will create code with terrible performance. Others will use clever optimization techniques.
Also, what's your goal? To be able to say: "Java sucks, everyone should use C++?" This is not particularly constructive. A more constructive approach is to build an application using the technology your team knows best (say Java), then find the bottleneck and find that it can be removed only by moving this part of the project to low-level language (including Assembler).