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
3 comments
4 u/Scaliwag 06 Jul 2015 13:10
x-posting:
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
2 u/skeeto 06 Jul 2015 13:51
C++ is in a rather unique position that its abstractions tend to boil away to nothing by being fully realized at compile-time.