I have been a hard core C++ fan, but I've recently started learning Java. I got frustrated at the fact that I couldn't get a straight answer out of the internet to the question: "How much faster is C++ than Java?" I was expecting a figure like 2 (C++ being 2 times faster than Java). I understand that it can be very difficult to give a single answer to the question - there are just so many variables, but I wanted to get some idea - some data-points from people who have done their own comparisons, or some comparisons involving larger programs.
I have a large in-memory database of a 'graph', consisting of about 400,000 nodes. My Java program was taking quite a long time to calculate the shortest path between 2 nodes. I didn't expect it to take so long, and I thought this must be evidence of Java being slow, so I wrote the same program in C++.
To my great surprise, Java outperformed C++ by 30% - a 30% drop in runtime.
This comparison is not what I would call a "significantly sized program", but it is larger than the programs you typically find reported with the micro-benchmarks. The A* algorithm involves:
I was fairly careful, and I do consider myself an expert C++ programmer.
In fact I tried to slant the comparison in favour of C++ in a number of ways:
BTW, I did try Java Persistence, but this was a mistake - it took 2 minutes to load the data-structure, whether it was from the raw XML files or from Java Persistence. The memory-mapped file on the other hand loads instantly if you've been accessing it recently.
Everyone assumes that C++ must be faster than Java, or at least as fast.
I know that people who write C++ optimising compilers are often bemoaning the fact that they could get huge performance improvements if they could assume "no aliasing", i.e. to make stronger assumptions about when variables are modified, than what the C++ language allows them. Developers of java compilers & JVM's on the other hand can make stronger assumptions about every aspect of a program. This is my main theory of why Java might be faster.
Another theory that Max Prakoso suggested is that the Visual Studio compiler doesn't have the benefit of being able to optimise the code with knowledge of its run-time profile. HotSpot on the other hand, does do this. I'm skeptical that this would produce a significant difference.
Max also asked me if I tried "whole program optimisation". I didn't initially, but it didn't make a huge difference when I did briefly try it. It might bring the difference down to 20%, but I should stress I didn't extensively test this variation.
Source-code is attached. Skype me on "drtimcooper" if you want the datafile.
I suppose it’s “hats off” to the HotSpot people.