Mail Archives: djgpp/1997/06/04/21:06:21
On Mon, 2 Jun 1997, Mark T Logan wrote:
>
> On Mon, 2 Jun 1997 16:29:29 +0200 eyal DOT ben-david AT aks DOT com writes:
> >I disagree on that also. Sometimes good OO design results in an even
> >faster
> >code.
> >Think about virtual function calls vs chains of "if-else" or "switch"
> >statements.
>
> Not necessarily. It doesn't matter how concise your source code is. If
> the
> compiler implements virtual functions with a lot of conditional jumps,
> then it
> doesn't matter what you use. Of course, I don't know how compilers
> implement
> virtual functions, so it may be that they are faster. Anyone care to
> comment?
Compilers generally place a hidden function pointer in the class structure
for each virtual function. Subclasses replace that function pointer with
one to their own definition of the virtual function, if the function has
been redefined.
Therefore, calling a virtual function requires a single pointer
lookup/dereference no matter how many times the function has been
redefined in subclasses of subclasses. Furthermore, the calling routine
never needs to be changed for a new subclass, whereas an if-else/switch
call would have to be changed in every calling function whenever a new
"subclass" is created. Finally, with today's pipelined processors it
shouldn't be hard to convince yourself that a pointer lookup/dereference
can probably execute much faster than a conditional jump.
---------------
Peter Berdeklis
Dept. of Physics, Univ. of Toronto
- Raw text -