tags:

views:

194

answers:

3

Hi all,

I'm reading up some material on whether Java can be faster than C++, and came across the following quote:

"Java can be faster than C++ because JITs can inline over virtual function boundaries."

(http://www.jelovic.com/articles/why%5Fjava%5Fis%5Fslow.htm)

What does this mean? Does it mean that the JIT can inline virtual function calls (because presumably it has access to run time information) whereas C++ must call the function through its vtable?

Thanks

Taras

+3  A: 

For what its worth, Java, C++, Assembly will provide relatively the same performance.

Yes, better performance can be acheived with handoptimzed C++, C, or Asm ... however, for the majorty of applications out there (try everything outside of serious graphics apps), that is not the bottleneck, -and- the lower cost of implementation makes up for any perceived lower performance.

Milan Ramaiya
+3  A: 

Since compilation of Java bytecode into machine code is deferred until runtime, it is possible for JVMs to perform profile-guided optimization and other optimizations that require information not available until code is running. This may even include "deoptimization", where a previously made optimization is revoked so that other optimizations can occur.

More information about this can be found under adaptive optimization on Wikipedia, which includes optimizations related to inlining.

Adam Goode
I understand there exist C++ compilers that can use profile information to determine whether to do these optimisations. The real problem with C++ is that the standard link model means the caller and callee are not necessarily compiled together.
Tom Hawtin - tackline
Yeah, new gcc can do this kind of link-time optimization (LTO).
Adam Goode
+4  A: 

The answer to your question is Yes: that is what the quoted text means.

The JIT will analyse all of the loaded classes. If it can determine that there is only one possible method that can be called at any given point, it can avoid the dispatching and (if appropriate) inline the method body.

By contrast, a C++ compiler does not know all of the possible subtypes, and therefore cannot determine if this optimization can be done for a (virtual) method. (And by the time the linker runs, it is too late ...)

Other answers have said that you can do this optimization by hand in C++ ... but that assumes that you (the programmer) can do the analysis yourself, and change methods from virtual to non-virtual. But if you get it wrong, you've got a bug to track down.

By the way, we can assume that this optimization is worthwhile for the average Java application. If it was not, the JIT compiler guys would not have implement it. After all, a worthless optimization is only going to make Java applications start more slowly.

Stephen C
+1 for answering the actual question
ykaganovich
A benefit is that getters and setters can be inlined meaning full abstraction protection (coding to interfaces and all) without automatically paying a penalty everytime.
Thorbjørn Ravn Andersen