As described in http://stackoverflow.com/questions/449827/virtual-functions-and-performance-c virtual methods may have an impact on performance (extra lookup in vtable, no inlining, ...).
But I was wondering, could the use of virtual functions speed up the linking process?
Suppose that I have a class X, calling a method of class Y.
- If the method is a non-virtual method, then
- the compiler has to look up the method in class Y to see whether it is valid, and how the call should be translated to assembly
- the linker has to find the method in class Y and replace the call address in the compiler-generated assembly by the address of the called method.
- If the method is a virtual method, then
- the compiler will also have to look up the method in class Y, and has to look up the vtable of class Y to construct the call (using the offset in the vtable)
- the linker has to do nothing anymore
It seems to me that when using virtual methods, the linker doesn't have to do much anymore, and therefore it will be faster (although I think the difference will be small).
Is this true? Does someone have experience with this? Was this ever tested?