c++performanceabstract-classvirtual-functions

Performance penalty for working with interfaces in C++?


Is there a runtime performance penalty when using interfaces (abstract base classes) in C++?


Solution

  • Short Answer: No.

    Long Answer: It is not the base class or the number of ancestors a class has in its hierarchy that affects it speed. The only thing is the cost of a method call.

    A non virtual method call has a cost (but can be inlined)
    A virtual method call has a slightly higher cost as you need to look up the method to call before you call it (but this is a simple table look up not a search). Since all methods on an interface are virtual by definition there is this cost.

    Unless you are writing some hyper speed sensitive application this should not be a problem. The extra clarity that you will recieve from using an interface usually makes up for any perceived speed decrease.