r/cpp • • 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

192 comments sorted by

View all comments

Show parent comments

1

u/coderemover Nov 03 '25

Update: mimalloc is even faster:

   Compiling mimalloc v0.1.48
   Compiling test-allocation-speed v0.1.0 (/Users/piotr/Projects/test-allocation-speed)
    Finished `release` profile [optimized] target(s) in 2.46s
     Running `target/release/test-allocation-speed`
Elapsed: 3886.279 ms
Elapsed: 3816.365 ms
Elapsed: 3793.933 ms
Elapsed: 3799.641 ms
Elapsed: 3803.768 ms

1

u/eXl5eQ Nov 03 '25

I used new but no delete in my previous test, which might confused you a bit. Note that I passed the new char pointer to vector<unique_ptr<char>>. The unique_ptr would take the ownership of the pointer, and delete it when the containing vector went out of scope.

Unlike the empty new Object in Java, I used 1 byte new char in C++, but I don't think it effects performance. Memory allocation must be aligned, and malloc don't support zero-size allocation anyway.

Now, skipping all other details, first I want to focus on your results. Unfortunately, Even with the same code, I'm unable to reproduce your results on my machine.

On my machine, rust is much slower than java. I don't know if it's caused by OS, compiler version, or hardware.

I've tried combinations various versions of JDK (18, 21, 25), GC (Z, G1, Shenandoah, Parallel) and heap size. ZGC in JDK 21 yields the best result, but interestingly, ZGC in JDK 25 works poorly. But even the poor JDK25 ZGC still outperforms rust-mimalloc.

Here's some numbers I got.

zulu-jdk21-windows +UseZGC, heap size set to 8G:
Elapsed: 4085.2536 ms
Elapsed: 2388.1038 ms
Elapsed: 2398.9338 ms
Elapsed: 2744.5762 ms
Elapsed: 2295.7608 ms
Elapsed: 2385.2412 ms
Elapsed: 1974.2356 ms
Elapsed: 2392.7326 ms
Elapsed: 1799.6501 ms
Elapsed: 1978.1949 ms
Elapsed: 1750.7008 ms
Elapsed: 1745.0567 ms
Elapsed: 1752.4087 ms
Elapsed: 1477.6801 ms
Elapsed: 1516.1547 ms
Elapsed: 2047.7199 ms
Elapsed: 1665.6646 ms
Elapsed: 1571.2061 ms
Elapsed: 1803.1032 ms
Elapsed: 1468.6294 ms

same java, 4G heap:
Elapsed: 11682.806 ms
Elapsed: 9821.4773 ms
Elapsed: 8907.0298 ms
Elapsed: 8600.4121 ms
Elapsed: 8867.3976 ms
...
Elapsed: 8927.1468 ms
Elapsed: 9347.7933 ms
Elapsed: 10290.9851 ms
Elapsed: 8966.4269 ms
Elapsed: 9074.8623 ms

jdk25, 16G heap:
Elapsed: 16108.9182 ms
Elapsed: 3884.1335 ms
Elapsed: 14165.0573 ms
Elapsed: 2938.6319 ms
Elapsed: 3176.7916 ms
Elapsed: 12937.412 ms
Elapsed: 20231.6433 ms
Elapsed: 15356.4182 ms
...

rustc-1.81.0, MSVC toolchain, release, default allocator:
Elapsed: 26810.952 ms
Elapsed: 27190.373 ms
Elapsed: 26872.211 ms
Elapsed: 27056.216 ms
Elapsed: 26850.303 ms
Elapsed: 26991.689 ms
...

same rustc, mimalloc:
Elapsed: 19586.458 ms
Elapsed: 19501.737 ms
Elapsed: 19628.396 ms
Elapsed: 19338.841 ms
Elapsed: 19380.584 ms
Elapsed: 19477.418 ms
...

Both rust cases consumes ~3GB RAM. I didn't explicitly configure the heap size for rust.

1

u/coderemover Nov 03 '25

Ok, I stand corrected, i missed the unique_ptr part. So there must be some difference between the toolchains. Your jdk numbers are fairly close to mine, but weirdly your Rust is a lot different. I’ll try the same benchmark on a different machine in my spare time ;)

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.

1

u/eXl5eQ Nov 04 '25

Yes, thats what I've said since the beginning: Java code sometimes can run much faster than C++, especially in some edge cases. But most of these performance gain comes with extra CPU and memory cost.

The CPU difference is often invisible in an everyday Java program, I could even show cases where Java use less CPU, but the memory difference is always obvious. It's common to see a 10x or even 100x memory consumption.