Does virtual function works slower than regular function in C++? Let's see.

Some test code:


virtual function performance c++

Apparently the answer is yes, it works slower. When user marks function as virtual, C++ compiler adds pointer to virtual table (array of function pointers to resolve inheritance hierarchy and polymorphic behaviours). Some processing time needs to be consumed to manage this.

For more information, here is virtual functions implemented in C.

Be careful with your abstractions. Still avoid premature optimisations!