A virtual call requires at least an extra indexed dereference and sometimes a "fixup" addition, compared to a non-virtual call, which is simply a jump to a compiled-in pointer. Therefore, calling virtual functions is inherently slower than calling non-virtual functions. An experiment done in 1996 indicates that approximately 6–13% of execution time is spent simply dispatching to the correct function, though the overhead can be as high as 50%. The cost of virtual functions may not be so high on modern architectures due to much larger caches and better
branch prediction. Furthermore, in environments where
JIT compilation is not in use, virtual function calls usually cannot be
inlined. In certain cases it may be possible for the compiler to perform a process known as
devirtualization in which, for instance, the lookup and indirect call are replaced with a conditional execution of each inlined body, but such optimizations are not common. To avoid this overhead, compilers usually avoid using virtual method tables whenever the call can be resolved at
compile time. Thus, the call to fn1 above may not require a table lookup because the compiler may be able to tell that derived can only hold a Derived at this point, and Derived does not override fn1. Or the compiler (or optimizer) may be able to detect that there are no subclasses of Base1 anywhere in the program that override fn1. The call to Base1::fn1 or Base2::fn2 will probably not require a table lookup because the implementation is specified explicitly (although it does still require the this-pointer fixup). ==Comparison with alternatives==