≡ Menu

Repost: It’s not about C++ and Java Performance.

Warning: anecdotal conclusions coming.

I think the crucial flaw in every Java v. language is that they always focus on, you know, specific code. “Here’s a fibonacci routine using recursion… in Java, it’s X, and in C++, it’s Y! Wow!”

Every time I see that kind of comparison, I’m left cringing. It’s crap. It’s interesting only if your skill level is pretty much identical to the guy writing the test code — in both languages. If your skill levels differ – if you’re better at Java than he is, or better at C++ than he is, the test might as well be written in Swahili (unless, of course, you read Swahili, in which case you should substitute another language, one you can’t read.)

Here’s what I have seen in the industry: if you have a guy who’s an average Java programmer and an average C++ programmer, the Java will usually have slightly better runtime characteristics… over time. It’ll have fewer bugs, better turnaround time for development, and an easier maintenance cycle. The Java runtime characteristics will be slightly better because Java punishes you for doing stupid things, and C++ does so at more problematic times, so Java will sort of hedge the average programmer in the right direction by default.

Now let’s take a master C++ programmer who’s a master Java programmer… and the resultant code will beat the ever-lovin’ crap out of the average programmer’s code. The master’s Java code will fly past the average guy’s Java, and his C++ code will cruise past the Java code. C++ would win here, almost every time, because a master knows how to generate optimal code, and while Java will do well… Java will rely on certain runtime characteristics and C++ will not, and a master can exploit both. The maintainability will probably go down in both (a bad thing), and bug counts will also typically be lower, because the guy is supposed to be a master.

Now let’s look at the next case: a poor C++ programmer who’s also a poor Java programmer. (Poor as in “skill level,” for clarification.) We’ll call him “Alan.” (Or “Kirk.”) Here’s where Java will shine: this guy will have trouble typically generating readable and runnable code in C++, whereas if he’s able to get Java to compile, he’s likely to at least runsomething. Here, because running something wins over not running something, Java will win almost by default… and the runtime speed will typically be atrocious regardless, but a successful run through C++ will still not be TYPICALLY as fast as the Java result. (The nature of the test still factors in, of course.)

So which one’s better?

Well, it depends! If you’re good at one or the other, that’s the one you should be using if nothing else factors in. If you’re a god at C++ and you’ve sort of maybe seen the Java specification once in something other than your native tongue… sheesh, use C++. If you rock at Java but still get dizzy when you see double colons and angle brackets in types – and you’re avoiding Java 1.5 like the plague – hey, Java’s your bag. If you’re Peter van der Linden and dream in C++ and Java at the same time, use what’s appropriate for the platform you’re on.

That’s the bottom line. Is it good enough? Do you know how to use it? Do you know enough about each one to understand the tradeoffs? You have to make your own decision based on your own skill level, not the skill of those ill-informed enough to go out and try to find a catch-all benchmark.

(Reposted from an old blog of mine, from June 2004, because I want the good stuff to be in the same location and because I keep seeing people read this and they’re reading it from the wrong place.)

{ 0 comments… add one }

Leave a Comment

%d bloggers like this: