Wednesday, June 16, 2004

Java vs C++: Which is Faster?

Via Slashdot, it transpires that a fellow by the name Mark Lea has updated the old "Java vs. C++ shootout" from way back in 2001, in which Java fared badly at the time; this time, though, things are rather different.

"I was sick of hearing people say Java was slow," says Keith Lea, "so I took the benchmark code for C++ and Java from the now outdated Great Computer Language Shootout (Fall 2001) and ran the tests myself." Lea's results three years on? Java, he finds, is significantly faster than optimized C++ in many cases.
There are some caveats to the test results, however, the most important of which is Lea's choice of compiler. Why GCC 3.3.1, instead of the Intel compiler or even the freely available Visual C++ Toolkit? As I've mentioned earlier, if there's one thing GCC is not, that is acting as a highly-optimizing compiler designed to squeeze the last ounce of speed out of a given platform.

The other point of contention with Mark Lea's tests is the choice of optimizer switches he made. In particular, he only opted for -O2 optimization, which is fine for most applications that spend their time waiting on data or user input, but far from optimal when high performance is important. He defends his choice by saying that -O3 performs space-speed tradeoffs, but that is hardly a worthwhile reason for not using that instead: hard drive space is cheap and continuously, rapidly getting cheaper, and most people would rather cope with a little storage bloat than spend more time waiting around for code to finish executing.

Having said all the above, I do think that these updated benchmarks indicate that Java is not quite the dog its reputed to be, at least not nowadays. As some of the Slashdot commenters point out, the JVM does have certain advantages over a C++ compiler, not least of which is that it can profile running code and optimize accordingly, something no statically compiled program can ever do. Another advantage Java has over C++ (and one Java shares with Fortran*), is that it lacks pointers, which makes code analysis and optimization a lot easier than it otherwise might be. Whether these advantages will outweigh the benefits of static compilation is something that must be ascertained on a case-by-case basis.

One thing that does stand out from the testing is how much faster the server JVM is than the client version in nearly all tests, so much so that Lea says the following:
They also show that no one should ever run the client JVM when given the choice ... [and] everyone has the choice.
*One final point: no one in his right mind who knew anything about high performance computing would ever write his numerical routines in C++. There's a very good reason why Fortran compilers are still in demand!


Blogger Isaac Gouy said...

Back in mid-2004 the Computer Language Shootout was revived, and continues to be updated, see

JDK -server benchmarks

The Computer Language Shootout Benchmarks

July 4, 2006 at 3:15 PM  
Anonymous Anonymous said...

Thank you!
[url=]My homepage[/url] | [url=]Cool site[/url]

July 20, 2006 at 10:11 AM  
Anonymous Anonymous said...

Thank you!
My homepage | Please visit

July 20, 2006 at 10:11 AM  
Anonymous Anonymous said...

Thank you! |

July 20, 2006 at 10:12 AM  

Post a Comment

<< Home