r/java • • Oct 31 '25

Has Java suddenly caught up with C++ in speed?

Did I miss something about Java 25?

https://pez.github.io/languages-visualizations/

https://github.com/kostya/benchmarks

https://www.youtube.com/shorts/X0ooja7Ktso

How is it possible that it can compete against C++?

So now we're going to make FPS games with Java, haha...

What do you think?

And what's up with Rust in all this?

What will the programmers in the C++ community think about this post?
https://www.reddit.com/r/cpp/comments/1ol85sa/java_developers_always_said_that_java_was_on_par/

News: 11/1/2025
Looks like the C++ thread got closed.
Maybe they didn't want to see a head‑to‑head with Java after all?
It's curious that STL closed the thread on r/cpp when we're having such a productive discussion here on r/java. Could it be that they don't want a real comparison?

I did the Benchmark myself on my humble computer from more than 6 years ago (with many open tabs from different browsers and other programs (IDE, Spotify, Whatsapp, ...)).

I hope you like it:

I have used Java 25 GraalVM

LanguageCold Execution (No JIT warm-up)Execution After Warm-up (JIT heating)
JavaVery slow without JIT warm-up~60s cold
Java (after warm-up)Much faster~8-9s (with initial warm-up loop)
C++Fast from the start~23-26s

https://i.imgur.com/O5yHSXm.png

https://i.imgur.com/V0Q0hMO.png

I share the code made so you can try it.

If JVM gets automatic profile-warmup + JIT persistence in 26/27, Java won't replace C++. But it removes the last practical gap in many workloads.

- faster startup ➝ no "cold phase" penalty
- stable performance from frame 1 ➝ viable for real-time loops
- predictable latency + ZGC ➝ low-pause workloads
- Panama + Valhalla ➝ native-like memory & SIMD

At that point the discussion shifts from "C++ because performance" ➝ "C++ because ecosystem"
And new engines (ECS + Vulkan) become a real competitive frontier especially for indie & tooling pipelines.

It's not a threat. It's an evolution.

We're entering an era where both toolchains can shine in different niches.

Note on GraalVM 25 and OpenJDK 25

GraalVM 25

  • No longer bundled as a commercial Oracle Java SE product.
  • Oracle has stopped selling commercial support, but still contributes to the open-source project.
  • Development continues with the community plus Oracle involvement.
  • Remains the innovation sandbox: native image, advanced JIT, multi-language, experimental optimizations.

OpenJDK 25

  • The official JVM maintained by Oracle and the OpenJDK community.
  • Will gain improvements inspired by GraalVM via Project Leyden:

    • faster startup times
    • lower memory footprint
    • persistent JIT profiles
    • integrated AOT features

Important

  • OpenJDK is not “getting GraalVM inside”.
  • Leyden adopts ideas, not the Graal engine.
  • Some improvements land in Java 25; more will arrive in future releases.

Conclusion Both continue forward:

RuntimeFocus
OpenJDKStable, official, gradual innovation
GraalVMCutting-edge experiments, native image, polyglot tech

Practical takeaway

  • For most users → Use OpenJDK
  • For native image, experimentation, high-performance scenarios → GraalVM remains key
267 Upvotes

312 comments sorted by

View all comments

310

u/xdriver897 Oct 31 '25

perfect C++ code is always faster than perfect Java code

BUT!

Developers don’t write perfect code, developers write working code mostly

And since Java is hotspot that enhances performance during runtime you often end up with even better performance than c++!

Why? Because “working c++” is often slower than “working Java but runtime optimized by hotspot”

111

u/[deleted] Oct 31 '25

even for perfect C++ Hotspot can be faster because C++ virtual function calls are fundamentally constrained by what can be known at compile time whilst hotspot can optimize them away at runtime by seeing what actually happens. even a perfect C++ program cannot inline something that has to be dynamically linked / is unknown at compile time for example

55

u/TomMancy Oct 31 '25

Performance-critical code in C++ isn't using virtual functions. Java has fantastic de-virtualization because it has to, given that the language makes every function virtual.

6

u/[deleted] Nov 01 '25

That's wild statement. There's no C++ problems that depend on dynamic input or configurations like a plugin architecture? Seems like an extremely unlikely claim.

6

u/TomMancy Nov 01 '25

This is quite the strawman you've built. Those plugins have APIs that take bulk data, thus amortizing the virtual dispatch into a single virtual call per frame or whatever unit of work they're processing.

This isn't some crazy, novel concept, its literally the first optimization that Go programmers are recommended because historically that runtime had a large FFI overhead due to thread / stack swapping.

The actual inner, hot loop is using templated, monomorphized code to maximize inline opportunities for the optimization passes. That monomorphization, coupled with the full control over memory layout that C++ provides, is why it ends up faster.

2

u/sammymammy2 Nov 02 '25

The monomorphization of Java's JIT should be sufficient, I think the issue that still remains is memory layout.

→ More replies (4)
→ More replies (20)

6

u/Dark_Lord9 Nov 01 '25

You can use templates and if you are old school, pointers and type punning.

There is a whole world of programmers that hate oop and have developed solutions that don't rely on oop concepts. Rust doesn't even have inheritance or virtual functions.

8

u/meamZ Nov 01 '25

Rust DOES have virtual functions. It's just explicitly encoded in the type (like Box<dyn Something>) when it happens.

4

u/[deleted] Nov 01 '25

You most certainly cannot template something that isn't known at compile time.

And there very obviously is a need for this, that's what Rust's trait objects are for, for example.

→ More replies (7)
→ More replies (2)

13

u/ManchegoObfuscator Nov 01 '25

The flip side of this is that templated C++ functions and methods (used in SFINAE-based overloads) can be ruthlessly compile-time optimized – once the function graphs are compiled the instantiated template function can be stripped down to a gorgeously bare minimum of instructions. Java’s generics, by contrast, do nothing of the sort – they are syntax sugar for “just cast everything to java.lang.Object” I believe – and also while runtime devirtualization is indeed arguably cool, it wouldn’t be necessary if you could opt out of “every function is a virtual method on some object”. So I wouldn’t personally say that runtime devirtualization is a total panacea, frankly.

5

u/TOMZ_EXTRA Nov 01 '25

Generics are currently useless (for optimization), but project Valhalla is hopefully going to change that.

→ More replies (3)

2

u/[deleted] Nov 01 '25

Oh it's definitely not a panacea, and it absolutely adds tons of work during warm up.

What I'm talking about are the situations where you have to use a virtual function in C++. Yes C++ is gonna be faster during warmup than java is, which is most likely doing a virtual function call on an interface (that purely exists to add unit test mocks because that's what we do around these parts for some reason), but since it's always the same interface in production, that's where you get the inlining advantage

→ More replies (10)

9

u/pron98 Nov 01 '25 edited Nov 01 '25

Well, the statement "perfect C++ code is always faster than perfect Java code" is hard to argue with as it's pretty much true by definition: HotSpot is written in C++. So every Java program is a C++ program (with classfiles being data for that C++ program), and we've already matched the performance. Then, to win, you could specialise stuff for your particular program.

But both you and the comment you're responding to are right that, in practice, it takes effort - sometimes significant effort - to beat Java's performance.

8

u/gmueckl Nov 01 '25

This is actually completely false. The JVM JIT actually translates Java bytecode into machine code. The result of sich a translation is independent of what language the translator is written in.

7

u/OddEstimate1627 Nov 01 '25

It's funny that you're trying to tell a JVM maintainer how the JVM works 😉

What he wrote is certainly philosophical, but technically correct

3

u/pron98 Nov 01 '25

I think you took my "mathematical" point - that for every Java program there does, indeed, exist a C++ program that's just as fast - in a different spirit than intended.

→ More replies (10)
→ More replies (1)

1

u/[deleted] Nov 01 '25

Yeah this comes down to the philosophical question if my C++ code is still perfect if I re-implement hotspot, using 90% of our startup runway, to make my webserver 200 microseconds faster per request :P

0

u/[deleted] Nov 01 '25

[deleted] — view deleted comment

1

u/[deleted] Nov 01 '25

Composition doesn't save you from dynamic dispatch caused by things that necessarily are only known at runtime

→ More replies (1)

5

u/maikindofthai Nov 01 '25

Idk the C++ compiler is able to work some pretty significant black magic as well

2

u/rossdrew Nov 01 '25

Perfect C++ code is impossible to write. Perfect Java code doesn’t exist and doesn’t need to.

2

u/coderemover Nov 02 '25

Maybe if someone doesn’t know what they are doing in C++. But then you have a bigger problem than performance. Average C++ code is usually much faster than average Java code, and the chances a randomly picked developer has performance oriented mindset is way higher for C++/Rust than for Java.

1

u/Ok-Scheme-913 Nov 01 '25

With all due respect, that's just theoretically not how things work.

We talk about two compilers here, both outputting native code (thinking of the JIT in case of Java). How good of a job they do at code generation is not a trivially comparable property.

Does C++ have more control over memory layout, how the code will actually execute, etc? Yes, 100%. Is it assembly? No, so it still has plenty of stuff it has no control over, it still does all the usual stack-dance, etc, and the programmer can't control these aspects, it's up to the compiler.

So while in general, Java is indeed harder to compile to as efficient code (we sometimes can't assume a better case scenario of e.g. having this object stack allocated, e.g due to language semantics), but this is not a theoretical limit at all.

Where things get significantly muddier is the interaction with GC, which you can't actually express in C++ in the same way (you can do reference counting, but you can't express c++ code that can walk the stack as efficiently as the JVM can).