Over a million developers have joined DZone.

Memory Locality in Java vs. C++

DZone 's Guide to

Memory Locality in Java vs. C++

· Performance Zone ·
Free Resource

Attempting to solve a problem involving analytics, a blogger and his colleague wrote similar programs in C++ and Java to address the same issue and then compared the performance of their competing programs. They found that the Java program performed 85 times slower than the C++, an astounding difference.

Java is not 85x slower than C++.  We can discuss about the relative speed of both languages, but it is nowhere near 85 times.  All I can say is that the web is full of small code examples where Java is as fast as C++.  Other comparisons using larger test programs, like this Google paper, and this comparison, say that Java is between 2x and 4x slower than C++, depending on the program used for testing.  This is consistent with my own experience.  So, we were facing something else that was causing a much larger speed difference.

The discrepancy was so significant that they decided to run a test to find the cause.

The use of the above data structure is what explains most of the difference in running time between C++ and Java.  We could now implement a similar data structure in Java to actually check that we get much faster running times.  However, given we have a correct and fast implementation in C++ we did not bother do it yet.  Indeed, we are now able to process graphs having 1 billion edges in about 8 GB memory.

What can we conclude from this discussion?  I'd say that if one cares about performance, then one should think about locality more than about programming languages selection.

Check out the full methodology at the original post.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}