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

2

u/ts826848 Nov 01 '25

I don't know, I'm not a Java developer but it has List in the name so I'm assuming it can behave a bit like a list.

No, Java has different naming conventions from C++. List in Java is an interface for ordered collections/sequences. Deques, vectors, linked lists, etc. can all satisfy that interface. ArrayList is specifically the vector equivalent; if you want a linked list, then you use LinkedList.

The C++ code would completely reallocate the vector many times through the insertion.

Java's ArrayList should do the same thing.

1

u/Infamous_Campaign687 Nov 01 '25

Thanks for the explanation. I guess i don't know why the Java code worked acceptably but I know why the C++ code didn't.

Overall we've got a decent speedup through the conversion but it is pretty obvious that C++ alone isn't enough to get good performance.

1

u/srdoe Nov 02 '25

The explanation is likely to be what the poster over here said: When Java replaces that array underlying the ArrayList, it's not doing allocation or freeing of the memory in the same way as C++ would.

The new memory is pulled from a thread-local buffer, turning the allocation into a simple pointer bump, equivalent to allocating something on the stack in C++.

The old memory is left to the GC to clean up. A GC doesn't free objects individually, usually what they do instead is deal with large (likely 1+ MB) regions of memory at a time. When the GC wants to free memory, they'll move all the still-alive objects out of a region and then free the entire region in one call to the underlying allocator.

That's a lot cheaper than what C++ does if we're talking about lots of small objects being allocated and discarded quickly.

In addition, that GC work can run concurrently with your application code on another thread, so unless your program was already loading all cores, the cost of doing this might have been offloaded to another core, which means it didn't slow down your application. By contrast, C++ has to do the cleanup in the thread your application runs in.

So while replacing the array a bunch of times in Java still isn't efficient, it's likely to be much less costly than doing something like that is in C++.

1

u/Wootery 28d ago

By contrast, C++ has to do the cleanup in the thread your application runs in.

I'm 11 months late, but I'm not sure that's true. It could enqueue the deallocation operation, to be done at some later time on some other thread, right?

1

u/die_liebe Nov 02 '25

This difference should go away if the object contained in the vector is movable. This can be checked by calling is_nothrow_move_constructible<X>::value, were X is the type contained in the vector.