Share

Comparing C++ and C (Inheritance and Virtual Functions)

We have covered C++ and C performance in a previous article. In this article we will dig deeper into C++ performance by analyzing the overhead of inheritance and virtual functions. 

Virtual Functions and Inheritance

This section presents the C++ code for a typical virtual function invocation scenario. This is then compared to the equivalent C code.

C++ code

C code

C code implementing the above C++ functionality is shown below. The code also includes compiler generated constructs like vtables. Virtual function access using vtables is also covered. (The presentation here has been simplified here to aid understanding).

Analysis

This section analyses the C++ code and its C translation and identifies the performance impact.

Object Construction Inheritance does increase the object construction overhead, as constructors for all the parent classes in the class hierarchy are invoked. There is the additional overhead of setting up vtable in the constructor.
Object Destruction Inheritance does increase the object destruction overhead, as destructors for all the parent classes in the class hierarchy are invoked. There is the additional overhead of setting up vtable in the destructor. Virtual destructors also increase the overhead of object destruction.
Virtual Function Invocation Virtual function invocation is slightly more expensive than invoking a function through a function pointer. In many scenarios, intelligent compilers can use normal method invocation instead of a virtual function invocation.

In a well designed object oriented system, a virtual function call would typically replace a switch statement so virtual function invocation might actually be faster than conventional coding techniques. For example, a generic draw statement in a "C" based paint program would involve switching over the type of shape and then invoking the corresponding draw function. In C++, this logic will be replaced by a virtual function call.

In our experience we have found that poorly designed and excessive use of constructors and destructors reduces performance much more than virtual function calls.

Memory Overhead Using plain inheritance has no memory overhead. Inheritance with virtual functions however does introduce the following memory overhead:
  • A vtable array pointer is added to all classes that use virtual functions.
  • Global vtable arrays are declared for every call with virtual functions (this should be a very small overhead).

 

Locality of Reference In the current computing environment, processor speeds have increased considerably but memory access speeds haven't kept pace. In such a scenario, cache hit ratio of an application plays a very important role in determining application performance.

In general this turns out to be an advantage for programs written in C++. With C++ code and data locality of reference is much better than C, as all the class code manipulating object data is located together. Also all object data is located at one place. In C code and data are scattered all over the place. Thus a C++ program should offer a better locality of reference than a C program. In many cases this might more than compensate for the performance overhead of C++.

 

Multiple Inheritance and Virtual Base Classes In this article we have not covered multiple inheritance and virtual base classes. There is a significant increase in overhead due to these features. We would recommend that you stay away from using these features.

Explore More