Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance penalty for working with interfaces in C++?

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

like image 660
andreas buykx Avatar asked Sep 22 '08 08:09

andreas buykx


1 Answers

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.

like image 99
2 revs Avatar answered Sep 19 '22 06:09

2 revs