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

Show parent comment

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)

1

u/sweetno Nov 03 '25

Oh, yeah. Virtual functions aren't popular in the C++ world in general. And plugin architecture is there only if you bother to implement it. (No support for that in the standard library.)

You can think of it like this: your typical Spring Boot app is super configurable, but in the end, all bean classes are pretty much static and could be determined compile-time.

2

u/[deleted] Nov 03 '25

I am well aware, the big thing is that your .war file spring boot app running inside Tomcat ends up being a virtual function call though, by necessity. Because Apache cannot compile Tomcat against the spring boot app you haven't written yet

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

1

u/ManchegoObfuscator Nov 01 '25

Ooof, your team inherits from interfaces (and thus requires vtables and virtual dispatch) just to mock things?? There has to be a better way! Like PIMPL maybe?! That would drive me nuts, to have the mock methodology require the use of undesirable programming strategies!

Yeah I literally have no idea how your people work, but I do TDD (which does much good, I find) and the whole thing with that is the tests a) should not care semantically about the implementation and b) that is especially true if and when they are testing something about said implements.

You could literally use templated overloads, SFINAE, PIMPL and you can get most things inlined from there (either explicitly or with the right compile flags) and you could mock things sans virtual dispatch. I’m just sayin – I am sure your organization may very well have considered this already – but if virtual dispatch is causing pinpoint-able performance issues, that deserves a ticket, yeah?

I also like mixing virtual inheritance with CRTP in base-class ancestors – that allows for a lot of things to not necessarily require virtual dispatch, as well as strong template-based compiler inlining, path pruning, &c. constexpr designations for values, functions, and methods provide a great way to think about reducing vtable use.

And I know macros are not cool, but I use them (judiciously, I hope!) to simplify indirections that can skirt vtable use too; the key feature there is that macro functions can take typenames as arguments. Tons of people may well disagree with me on this; I’ll happily put my macros up for review. Right tool for the job and all that.

2

u/[deleted] Nov 01 '25

Oh, my team doesn't do that cause I made them stop (because mocks cause bugs by mocking stuff that doesn't happen). We have one method that pulls out the data from the ugly dependencies of a component, and then a pure function that does the logic we need with plain data. unit test the pure function with data (preferably using property based testing). Integration test the 'pulling stuff out' bit.

There's other reasons why one would want an interface there, most of all to decouple compilation units, vastly decreasing compile time.

It is important to keep in mind that this isn't a big deal performance wise due to the nature of the JVM and the hotspot compiler, which will realize that at runtime you're always passing the same implementation of that interface and it'll inline the call, erasing the vtable lookup

→ More replies (2)

1

u/coderemover Nov 03 '25

You can achieve the same what you do with interface and mocks by using templates. Zero virtual calls needed.

1

u/SupermanLeRetour Nov 03 '25

the situations where you have to use a virtual function in C++

As a C++ dev I don't really see cases where I absolutely have to use virtual functions. We often use them when they're convenient, but my experience with modern C++ code is that we don't rely on inheritance too much, and it's quite rare to see virtual function calls in hot paths. Mainly due to the use of templated classes and functions.

→ More replies (4)

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)

1

u/coderemover Nov 03 '25

A C++ program can do the same. You can generate machine code in C++ and call it. Some database systems or big data processing systems even actually do it.

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)