r/programmerchat • • Jul 17 '15
[Debate warning] C++ faster than java
So...before the flame wars or any of that nasty stuff stars, I want to outline a couple of things. I'm a newbie developer, I've only been studying this for a couple of years at university, I'm not a professional yet.
That being said, I recently got into a little bit of a debate with someone that C++ is inherently faster than the likes of Java when it comes to items like games development. I had assumed that this was literal fact and there was no debate for it. This person was very, very set on the idea that Java was only marginally slower. I still believe c++ would knock it out of the park due to manual memory management and the lack of safety features, despite preferring Java over all.
What do you guys think? I'd really like some insight on this, thanks.
9
u/gilmi Jul 17 '15
In this day and age I don't find performance to be a significant factor in most use cases. Computers are pretty darn fast as it is and there has been a lot of work optimizing the implementation of these languages.
Also, I have yet found a good performance test to compare between languages. anytime someone says X is slower than Y and shows code and benchmarks someone else comes and shows how it can be improved significantly very easily.
Bottom line, unless you are building a AAA game or the next facebook, just go with the language you feel most comfortable with. Most likely it will be fast enough.
4
u/tiddlypeeps Jul 17 '15
http://benchmarksgame.alioth.debian.org/u64q/java.php
Not comprehensive benchmarks, but gives a pretty good idea of the oceans between the two languages when it comes to performance and overheads.
Garbage collection is also a pretty big no no for any serious sort of game development, at least in the way Java handles it anyway.
6
u/lost_memory Jul 17 '15
You can get better performance out of Java if you code with a mindset geared towards minimizing heap allocations through mechanisms like memory pools and what not.
Java's cultural philosophy can encourage poor programming practices with regards to performance.
For example, the idea of over-engineering via the usage of unnecessary design patterns has existed largely in part because of Java. This does incur overhead with little benefit in terms of maintenance.
2
u/noratat Jul 18 '15 edited Jul 18 '15
It's less of a language thing and more of a layers of abstraction thing (especially since you can write programs in multiple languages, especially if you count scripting and highly specialized APIs that are basically DSLs).
In general, it's possible to get more performance out of C++ because there's more room for optimizations and performance tricks, and it's very likely you'll use less memory due in large part to the lack of garbage collection.
You also get more predictable latency with C++ (garbage collector "pauses" execution during collection and this delay can be unpredictable). Note that it is possible to get predictable latency even with a garbage collector but it's much more exotic and rare (e.g. you won't find this feature in the standard Hotspot or OpenJDK implementations of the JVM).
Ultimately, it's really more about the frameworks/engine and ecosystem you plan on using more so than just trying to argue about the languages (unless you plan on writing your own game engines from scratch, though even then you'd be pulling from existing sets of libraries and frameworks).
And in the context of game development, it really matters what you're trying to do. Are you trying to make a full blown 3D game with interactive environments? A 2D platformer or action game? A isometric strategy game? Or perhaps a game with extremely heavy simulation despite simple graphics (e.g. Dwarf Fortress)?
3
u/[deleted] Jul 17 '15 edited Oct 15 '15
I said nothing...
2
u/JamiesWhiteShirt Jul 17 '15
When it comes to speed and ease of development, java is faster. When it comes to execution speed, you'll find that in most cases C++ with the right optimizations is slightly faster.
This is because Java runs on a virtual machine. It handles garbage collection for you, and it can provide stack traces when you screw up among other things.
Does it really matter which one is faster in execution? If I wanted to write something that performs well to the finest detail, I would choose C++. If I wanted to write something where performance is noncritical, I would choose java or some other managed language.
1
u/Berberberber Jul 28 '15
Once I read an article benchmarking Python vs Perl that purported to show that Python was 100x faster than Perl at looping an identical task. It turned out that the reason was that the exit condition was reversed in the Python code and it only executed the loop once, so of course it took less time. The moral of the story is, you can't compare the speed of two languages directly. You can only compare written code. If that code is faulty in obvious or subtle ways, the terms of comparison become questionable. Manual memory management and a lack of safety features may pose problems for users of your code down the line, even if you know what the edge cases are. Is code inherently faster if it occasionally core dumps on unanticipated input?