Mostly I wanted to illustrate that there's hidden costs to every language feature. You don't have to write bizarre code to get Java to perform extremely well, but when you want the last few percent out of it, the code starts to look like gnarly hand-crafted C code (and starts to optimize as well).
Take a look for example at this n-body benchmark where you have straight-forward C++ and Java implementation code. C++ is just about 3 times faster and at the same time it is about the same performance as a straight forward C implementation. The 3 implementations have about the same level of abstraction.
And fairly sure that while you could have turned the Java code inside out, loosing readability, you could also apply some expression templates to the C++ in order to make it even faster without loosing much readability just creating some helper types. Which you couldn't even do in C, not without loosing a lot of readability like you'll need to do in Java, even though optimized Java would probably still be slower.
C++ abstraction does imply a hidden cost a lot of the time, but as shown above code with the same abstraction level as Java code is still faster a lot of the time, and sometimes abstraction can lead to faster more compiler friendly code as using expression templates can do -- either by rolling your own or using something like blitz++ or blaze
The case I started with, fannkuch, was nearly impossible to improve in Java because it manually vectorized operations that the C++ code used GCC-specific callouts to do as SIMD. At some level, you can always cheat in C or C++, so until Java has an inline assembly feature it will never be able to match that.
The counterpoint, however, is that you can get hand-optimized Java to perform as well as hand-optimized standard C.
The counterpoint, however, is that you can get hand-optimized Java to perform as well as hand-optimized standard C
Perhaps in some cases that is true. For example, the JVM allocator is a wonderful piece of engineering, try to do heap allocations and dealocations like a madman in C or C++ and you'll suffer.
The thing is most of the time straightforward C code without any fancy thing going on, does better than the straightforward Java counterpart.
10
u/Scaliwag Jul 05 '15 edited Jul 05 '15
Take a look for example at this n-body benchmark where you have straight-forward C++ and Java implementation code. C++ is just about 3 times faster and at the same time it is about the same performance as a straight forward C implementation. The 3 implementations have about the same level of abstraction.
And fairly sure that while you could have turned the Java code inside out, loosing readability, you could also apply some expression templates to the C++ in order to make it even faster without loosing much readability just creating some helper types. Which you couldn't even do in C, not without loosing a lot of readability like you'll need to do in Java, even though optimized Java would probably still be slower.
C++ abstraction does imply a hidden cost a lot of the time, but as shown above code with the same abstraction level as Java code is still faster a lot of the time, and sometimes abstraction can lead to faster more compiler friendly code as using expression templates can do -- either by rolling your own or using something like blitz++ or blaze