c++performanceoptimizationvirtual-functions

Virtual functions and performance - C++


In my class design, I use abstract classes and virtual functions extensively. I had a feeling that virtual functions affects the performance. Is this true? But I think this performance difference is not noticeable and looks like I am doing premature optimization. Right?


Solution

  • A good rule of thumb is:

    It's not a performance problem until you can prove it.

    The use of virtual functions will have a very slight effect on performance, but it's unlikely to affect the overall performance of your application. Better places to look for performance improvements are in algorithms and I/O.

    An excellent article that talks about virtual functions (and more) is Member Function Pointers and the Fastest Possible C++ Delegates.