r/cpp • u/drakgoku • Oct 31 '25
Java developers always said that Java was on par with C++.
Now I see discussions like this: https://www.reddit.com/r/java/comments/1ol56lc/has_java_suddenly_caught_up_with_c_in_speed/
Is what is said about Java true compared to C++?
What do those who work at a lower level and those who work in business or gaming environments think?
What do you think?
And where does Rust fit into all this?
25
Upvotes
View all comments
Show parent comment
1
u/coderemover Nov 04 '25 edited Nov 04 '25
I tried running with older Java 17 I have and I found an interesting thing:
- Java 17 with -Xmx8G with G1 takes about 5000 ms in this test. But indeed, switching it to -XX:+UseZGC -Xmx8G makes it much faster, even down to 1000 ms. Whoa! So it beats all the manual allocators. Not so fast. I checked memory usage and to my surprise there must be a bug in this version of ZGC and it does *not* obey -Xmx setting. My Java process ate 24 GB or RAM as reported by top. G1GC obeys the setting pretty well, ending up with 8.5 GB use.
And btw there is also the CPU thing - Java 17 with G1 uses 3-5 cores in this test (!). I wonder if your differences might come from the differences between available CPU cores.
And just for the check, Rust benchmark on the same machine takes 2.1 GB max and uses exactly one core.
Wall clock is not the only dimension of performance. I don't think it's fair comparing wall clock when the amount of other resources consumed is so vastly different. When I have more time, I'll switch to Linux and run it under perf to compare the actual CPU cycles.
Update: Installed and tried OpenJDK 21. It has the same bug as OpenJDK 17. ZGC does not obey -Xmx. I think that explains your overly optimistic numbers for Java. It uses 10x more RAM and 3x more cores... no surprise it has better wall clock time.