Re: Non virtual and inheritance
Nobody <nobody@nowhere.com> wrote:
Virtual calls are more computationally expensive
Actually if you actually measure the speed difference between calling
a regular (non-inlined) function and a virtual function, the difference
is barely measurable, if not inexistent. Yes, there's an additional
indirection step, but its impact on the overall speed of the function
call is quite minimal (especially if the function takes parameters and
does something that takes many clock cycles).
In short: If you are avoiding virtual functions because you fear that
your program will become slower, then you are worrying for nothing.
The actual problem with virtual functions (which is only a problem in
certain cases) is that a class having virtual functions will be larger
(typically by a pointer) than a class with no virtual functions. In
most cases this doesn't matter, but if you are super-tightly optimizing
the memory usage of a very small class, that size increase can be crucial.
(If eg. your class only has eg. an integral as a member, making any of the
functions virtual will double the size of the class.)
(Virtuality may also impede or reduce the effectiveness of function
inlining, but that's also something that is relevant only in certain
cases.)
This overhead is useless if you don't need virtual functions for anything.