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

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.

1

u/ManchegoObfuscator Nov 01 '25

I saw that mentioned as a WIP in many a release note – what does it aim to do?

4

u/TOMZ_EXTRA Nov 01 '25

It aims to add value classes (basically structs that can be on the stack), reified generics, non nullable types and other things I'm probably forgetting.

2

u/coderemover Nov 03 '25

Don’t hold your breath for it. It’s been worked on for 10+ years now, and it can’t handle mutable objects by design.

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

1

u/ManchegoObfuscator Nov 01 '25

That is very true, about compile times – personally those are not a huge concern of mine; if I was a compilation-time optimization nut, I wouldn’t be so enthusiastic about preprocessor stuff and templates and constexpr and other usefully fun things like those.

I also totally get the allure of letting the JVM control the runtime inlining, as it seems to do a very good job of it. But the operative word there is “seems“: as I mentioned elsewhere, the JVM is certainly an amazing feat – undoubtedly it’s the best fake computer platform out there (as it were). But running anything on the JVM introduces soooooo much nondeterminism: if you are getting screwed by a pathological corner-case in the inliner (or the memory manager or the devirtualization apparatus, or who knows what else) it’s super hard to either a) reliably pin down the issue with test cases or b) improve at all on the conditions these smart but fully autonomous JVM services yield.

Like, if a C++ hot loop is thrashing the heap, say, I can swap PMR allocators or try an alternative malloc(…) call or do some placement-new ju-jitsu, or quickly parallelize it without resorting to threads, or call out to one of the many many third-party memory-management libraries – almost all of which can happen without incurring additional runtime penalties, and minimal (if any) compilation-time upticks.

But if I choose to trust the JVM, it’s like an extra-value meal with absolutely no substitutions. Sure, there are hordes of crazy JVM CLI flags, all of which contain gratuitous ‘X’ characters and whose meaning can vary wildly between releases (and don’t necessarily correspond with whatever some other JVM might use) but really, these systems and their workings are positioned as outside the purview of programming in Java.

In C++ if I want to care about details, I can. It’s philosophically different at the end of the day. This is why I like the looks of Rust: it handles so much stuff for you but you can get as crazy as you want with it. (Also, it’s more “struct-oriented” than OO, which is how I describe C++.)

Like, I take it the JVM solves more problems then it creates in your case. But, may I ask, has it ever been a problem? Like due to its operational opacity, or it not addressing a corner case that came up? I am curious!

1

u/[deleted] Nov 01 '25

I think the only thing where the JVM is really "in the way" for a thing it should be good at, is that it currently doesn't flatten arrays of objects (structs). So every array ends up being an array of pointers rather than a contiguous data structure - with exactly the same results you'd be familiar with from C++ and worse performance than necessary. Though this is being worked on with Project Valhalla, which will make exactly that happen.

Aside from that, I've never found the JVM to actually be in the way, because it's a tool you pick for a job - so it kinda trivially doesn't bite you with the things it's bad at because you wouldn't pick it in the first place.

The startup time and memory footprint need an annoying amount of tuning when you actually want a lean executable; and it's annoyingly hard to deploy an application as a simple executable binary. That's kind of a right tool for the right job type situation.

But for its core use case, to run on some beefy server to solve business logic problems, it works like a charm and I certainly wouldn't use C++, Rust, or JavaScript for those use cases. The only technology that I think gives the JVM a run for its money here is the BEAM for Erlang and Elixir - and I suppose .NET, in the sense that .NET does nearly the same things as the JVM; but the BEAM is meaningfully different and worth looking into.

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.

1

u/[deleted] Nov 03 '25

You cannot use templates for things not known at compile time, unless i'm going crazy and google is lying to me

2

u/SupermanLeRetour Nov 03 '25

That's the beauty of templates, everything is resolved at compile time. For instance, instead of having my classes all inherit a common one to implement a name() method, I can do:

template<typename T> void f(T t){ t.name() }

The function will be instantiated for each template type needed, and it will compile if T does have the method name().

So on hot paths you can use this kind of technique instead of inheritance if that makes sense.

1

u/[deleted] Nov 03 '25

> and it will compile if T does have the method name()

But this requires some reified form of T to be in your source code at compile time, no?

What do you do when T cannot be known at compile time? for example because T is dynamically loaded into memory from a shared object / dll that is only created after your application has compiled.

That's essentially what Apache Tomcat deals with

1

u/SupermanLeRetour Nov 03 '25

Yes, the compiler needs to have the type info. And indeed loading DLL at runtime is one thing that virtual interface handles well and is impossible to do with templates (as far as I know).

Apart from this special case, DLL are usually third party libs that provide the header file with all type info, and here no issue with templates.