This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

 

  Improving Performance Of Virtual Functions
  Submitted by



One of the things that can end up costing performance is the overhead of calling the base class in a virtual function. One trick I've used for that is inline virtual functions, which may sound contradictory but it's not. Consider:

class Foo
{
    virtual bool IsA( TypeID Type ) { return Type == TypeFoo; };
};

class Bar : public Foo { virtual bool IsA( TypeID Type ); };

void Bar::IsA( TypeID Type ) { return (Type == TypeBar) || Foo::IsA( Type ); }


Since the compiler knows exactly which function to call when you say Foo::IsA it can inline the definition of the function.

-------------------------------------------------------
Don Neufeld - Programmer - Verant Interactive St. Louis

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

 

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.