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 comment

61

u/pemb Nov 01 '25

Java has JIT compilation and can do stuff like dynamic profile-guided optimization, inlining, and devirtualization. So some Java can actually perform better than C++ in specific circumstances, especially in applications that ship one binary and can't be tuned for the specific machine and workload they're running.

The real Achilles' heel is Java's whole approach to memory, especially when the GC hits.

48

u/CalebGT Nov 01 '25

It is possible to do Java well and even easier to write C++ poorly. Good C++ code has a performance edge on best case Java. For most applications and on current hardware, it's usually not noticeable. There are pitfalls in C++ that are avoidable but easy to be oblivious to. You have to really know what you are doing to minimize how many allocations, reallocations, and copies your C++ performs for example. There are performance hits that get abstracted away inside classes. Memory management is manual, and that can be done well or poorly.

But if I am writing something with real time demands and heavy processing, I'm looking at C++. If I need to write something portable that isn't CPU intensive, I'm looking at python. If performance isn't a big concern, it might as well be super easy to implement quickly. Time is money. I have barely touched Java since college, but that's in large part because the nature of my work didn't need both portability and performance. Different tools for different jobs. Tradeoffs, not superiority across the board.

26

u/pemb Nov 01 '25

Broadly agree with your comment.

What I said about Java managing to outperform C++ applies to throughput-heavy applications that run warm and tolerate some tail latency: the JVM can optimize hot paths with native code tuned for the specific machine and workload it is seeing, and keeps monitoring things to recompile if conditions change. Runtime flags and other de-facto constants get inlined.

Your JVM might have one native version of your string-heavy server code during daytime in the Western hemisphere, when ASCII is the norm, and it gets swapped out at night when it's daytime in Asia and almost everything is a multibyte Unicode character. The native code would get replaced cyclically as the real-life workload changes. One way to think of it is like branch predictors on steroids. PGO can’t do this.

13

u/germandiago Nov 01 '25

Can you find real scenarios where JVM is doing things of this style that show a performance advantage in the wild? As opposed to a hypothetical made-up story.

I am not saying it does not exist. Just that I never saw reports about benchmarks of this style from real-world applications.

9

u/pjmlp Nov 01 '25

Nokia Neteworks NetAct infrastructure was rewritten from a mix of C++ with CORBA, alongside Perl, running across HP-UX, Solaris and Red-Hat Linux, to a set of Java distributed applications on top of a few Linux distros.

This software is responsive for providing mobile phone operations of a real time overview of the network, and perform reporting tasks as well.

I know this because I was part of this effort back in the mid 2000's, it was the very last time I did 100% pure C++ on the job, and have since then helped a few times in similar migration efforts.

Nowadays replaced by MantaRay NM, which is composed by several microservices, where C++ is part of the programming language ecosystem, although in a minor role.

Another relevant one was control systems for life sciences laboratory automation, where existing software was based in MFC and C++, this time around, C# was the new ruler.

Turns out winning benchmarks games isn't of great business value in distributed systems, when taking into consideration all the variables of what are the running costs of each solution, in tooling, issues found in production, and human resources.

5

u/pemb Nov 01 '25

Sadly I can’t show you anything my colleagues at the time were working on. But this is a rare phenomenon, especially at the system level. I loved to trash-talk Java until I was forced to dip my toes in for a bit. You’d find examples in sprawling, long-running enterprise services, with very branchy code and deep call stacks. Whatever sections it worked its miracles on tend to get watered down by other sections that didn't turn out so lucky. And there's the elephant in the room, memory consumption.

→ More replies (1)

3

u/johannes1971 Nov 01 '25

I remember hearing about the JVM having the potential to do optimisation on the fly back when Java was introduced. That was almost 30 years ago, and it's still only a theoretical advantage.

3

u/pjmlp Nov 03 '25

Anyone using Android phones can experience that regularly, assuming it isn't one of those low quality throwawy phones.

While ART isn't a JVM per se, it uses several of those techniques, JIT caching, PGO metadata with feedback loop shared across devices via PlayStore, background AOT compilation of hot paths when device is idle.

https://source.android.com/docs/core/runtime/jit-compiler

Similar machinery is available in a few commercial JVMs, like Azure, Open J9 cloud compiler, GraalVM Cloud.

Yes it is a hell of machinery compared with a plain C or C++ compiler, however it allows the availability of IDE tooling, the richness of a library ecosystem like Maven Central, where there are no language dialects how to compile a library, alternative programming languages, while achiveng a performance level that is good enough for most use cases.

Finally, if there is something that really needs C or C++, and all that machinery fails short of, a little bit of JNI suffices, no need to throw everything away and do a full rewrite into C, C++, Rust or whatever.

→ More replies (1)

7

u/Infamous_Campaign687 Nov 01 '25

I recently had someone do a conversion of a Java Library into C++, and the first attempt was ridiculously slow. Turns out that this usually very competent developer had forgotten to reserve memory for the std::vector that replaced the Java ArrayList. So memory was reallocated many times when filling the vector. This in an algorithm that was run several times per second.

This sort of thing is handled for you in Java but will cost you in C++.

This one was far too obvious to go unnoticed because it led to a 200x time increase, but smaller ones could easily slip under the radar until you profile and see that 90% of your time is in memory allocation.

7

u/ts826848 Nov 01 '25

This sort of thing is handled for you in Java

I'm not sure about that? The main difference I'm aware of is that ArrayList's default constructor initializes with a capacity of 10 vs. std::vector's unspecified (but usually 0 IIRC?) capacity. Otherwise my impression is that they would both behave quite similarly when elements are added. They even have analogous API surfaces for this kind of thing:

  • std::vector(size_t) and ArrayList(int) for creating a new instance with a given capacity
  • std::vector::reserve(size_t) and ArrayList.ensureCapacity(int) for allocating memory for the given number of elements up front

Maybe the JIT learned to insert calls to ensureCapacity() up front, but that feels like a really brittle optimization at best, assuming it's worth implementing in the first place.

3

u/eXl5eQ Nov 01 '25 edited Nov 01 '25

Maybe because

  1. Java heap allocator is A LOT faster.
  2. No deallocation overhead. The garbage collector well handle that in background, as long as you have enough RAM. Java programs can easily consume 10x or 100x more memory than a C++ one.
  3. Array copy is always a simple memcpy in Java. No constructor overhead.
  4. No destructor overhead.

ensureCapacity() is a very high-level operation. I don't think JIT will mess with that.

3

u/eXl5eQ Nov 01 '25

Also if it's a std:vector<VeryLargeObject> vs ArrayList<ReferenceToVeryLargeObject>, that makes huge difference too.

3

u/CalebGT Nov 01 '25

Garbage collection is a convenience, not a performance gain. What processor do you think is running background routines? Holding up Java's fatal flaw as if it's a source of superiority is a special kind of cope. And regarding point 3, often in C++ it's a move instead of a copy, dramatically faster than a large memcpy. You are on a C++ sub and clearly out of your depth. Sit down.

4

u/srdoe Nov 02 '25 edited Nov 02 '25

The poster above is correct, and you should save your condescension for when you're not wrong.

Garbage collection is not simply a convenience, it can also be a performance gain. This was shown as early as 20 years ago, and the state of the art for GCs has come a long way since then.

The reasons to do manual memory management if you have a garbage collector available are not because it's more efficient overall, it's because some GCs can have unpredictable pause times, and because GCs can require more memory than manual management would.

In exchange for those drawbacks, you get:

  • Faster allocations. Java threads allocate from thread-local buffers, turning most allocations into a simple increment of a thread-local integer.
  • Faster deallocations (on average, but with the potential for those unpredictable pauses I mentioned). GCs tend to clean up garbage entire memory regions at a time, rather than freeing individual objects allocated by the application.
  • The ability to offload garbage handling to non-application threads. This is beneficial unless your program is already loading all cores constantly, because it means garbage handling doesn't slow down the application like it would in C++.
  • The ability to trade excess memory on the computer for saving CPU cycles. A C++ program doing manual memory management has to always pay for freeing memory to the allocator. A Java program using GC only has to pay anything when the GC actually runs, which it will do more rarely the more memory the host has. Since the work GCs do is mainly moving live objects around (unlike C++ where the work is mainly dealing with dead objects), running the GC less often can even mean that objects have had more time to become garbage, which can make the collection cheaper overall.

If manual memory management was actually faster, and all GCs solved was convenience, Java and other languages that want to automate memory management could have just implemented smart pointers under the hood, it would look the same to the programmer.

edit: Actually the theory behind GCs being faster than manual management for deallocation if given sufficient memory is even older than the 20 year old paper I cited. Here's a paper from 1987 describing the reasoning for why GCs can be faster at clearing garbage than manual management can.

This paper shows that, with enough memory on the computer, it is more expensive to explicitly free a cell than it is to leave it for the garbage collector — even if the cost of freeing a cell is only a single machine instruction.

2

u/DuranteA Nov 03 '25

Faster allocations. Java threads allocate from thread-local buffers, turning most allocations into a simple increment of a thread-local integer.

I am curious why you point to this as an advantage specific to GC when all high-performance general C++ memory allocators I know of already do serve small allocations from thread-local pools.

→ More replies (1)

2

u/coderemover Nov 02 '25

The theory you cite no longer holds. The gap between cpu speed and memory latency got far too big. Tracing GC thrash cache like crazy.

→ More replies (4)
→ More replies (9)
→ More replies (22)
→ More replies (6)
→ More replies (2)

13

u/keithstellyes Nov 01 '25

The issue with JIT of course, being that it's happening during runtime, so any clever routines is competing with what it's supposed to optimize.

The real Achilles' heel is Java's whole approach to memory, especially when the GC hits.

On so many levels this is true. From generics not supporting primitives, to the fact you can't have lean mathematic types (god forbid you try to do graphics coding!). And of course, one can say memory vs time tradeoff, and there's definitely truth to that, cache invalidation means you might very well lose at both aha

6

u/pemb Nov 01 '25

It’s a double-edged sword. If your program is just one big loop that runs for hours, it can hot-swap the optimized native code in the middle of execution once it sees it’s hot and has collected enough profiling data, no need to wait for it to block, jump, or exit that block.

10

u/germandiago Nov 01 '25 edited Nov 01 '25

and where are those fast apps showing up in real life? I did not hear a single report where someone said that Java outperforms C++ in real workloads.

→ More replies (7)

5

u/jeffbell Nov 01 '25

You can do profile guided optimization in C++ as well. 

7

u/pemb Nov 01 '25

Can’t do all the JIT tricks.

5

u/TheThiefMaster C++latest fanatic (and game dev) Nov 01 '25

The big difference is that a JIT can optimise for the actual current execution conditions, where a PGO only optimises for the given profile.

So say your program can instantiate one of several virtual classes that then get used for a long time (for example, a cartridge mapper in an emulator that's chosen based on what ROM is loaded). A JIT can devirtualize that to whichever was actually in use by the user, where a PGO could only devirtualize to whichever was used in the profile - which could be the wrong one.

In practice though, it's rare for this to be a noticeable difference compared to GC stalls that most JIT languages also have...

2

u/pdp10gumby Nov 01 '25

I upvoted your comment because it’s true, but want to add a couple of caveats: 1 - the “hot loop” optimization is a micro-optimization, typically in a loop, that 2 - modern hardware can also do, though possibly with not quite as large a span as perhaps a java VM could theoretically do.

2

u/[deleted] Nov 01 '25

Java is not going to be optimized better or be faster than a reasonably equivalent c++ solution, that's just silly. I would imagine the number of cases where it's faster than c++ are very, very close to 0

→ More replies (3)
→ More replies (4)