Posts mit dem Label virtual function werden angezeigt. Alle Posts anzeigen
Posts mit dem Label virtual function werden angezeigt. Alle Posts anzeigen

Mittwoch, 20. Februar 2008

Virtual Function Table

Every now and then you read in books or magasins that C++ gets slow when virtual functions are used, because it must be determined at run-time which function must be executed.
Well, I wrote a set of classes and a test program to measure the performance of the following different implementations:
  • Class without any virtual function.
  • Class with a virtual destructor.
  • Class where the function that's called is defined virtual.
  • Class with a virtual destructor and the function defined as virtual.
  • Class derived from a base class (interface-like) which defined the function as pure virtual, and the implementation in the derived class.
Since the test function itself didn't do much I had to call it several million times to get a run-time in seconds for every test. And the result was: No difference at all, every type of implementation showed the same run-time behaviour, the difference between the different measured times was less than 1 percent.

So, use base classes and virtual functions and all the fancy features that C++ offers!