[SDL] Re: BeOS and SDL

Pierre Phaneuf pp at ludusdesign.com
Fri Aug 4 15:09:17 PDT 2000


Mattias Engdegård wrote:

> > Inheritance is faster than a switch, usually. Inheritance works like
> > function pointers, there is a small overhead, but the decision-making is
> > all done at once (when the object is created).
> 
> Most modern CPUs do a fair job on predicting branches, but most do not
> at all as well in the indirect function call prediction business.
> (Most tests I've done confirm this.)
> 
> When you compare a switch to a virtual function call (which is basically
> a indirect function call + the cost of fetching the pointer from the vtbl),
> it depends on what code is emitted for the switch, how many branches etc.
> 
> Compilers often emit branch trees and/or jump tables for switches,
> depending on the number and distribution of the case labels.
> So the right answer is the one you don't want: "It depends" :-)

Unfortunately, you're right. :-)

*But*, if I understand this correctly, this is already going through a
C++ virtual method, so the cost of replacing that one with another one
through inheritance would be precisely "zero". ;-)

In a general manner, I would say that the cost of a virtual method call
is stable, and that for a switch it is variable. If the switch is small
enough, it probably is faster, but as it gets larger and larger and that
more tests needs to be done, they become slower. A virtual function call
costs the same, no matter how many classes implement it.

-- 
/* you are not expected to understand this */
 -- from the UNIX V6 kernel source



More information about the SDL mailing list