I had to design and develop a C++ module which will be used in a real time environment (it will be run on a modern multi-core PC). When I designed it I created C++ interfaces (classes with only pure virtual member functions) and I used dependency injection in order to be able to test it with Google Mock Framework. I know that this approach has run time performance overhead compered to the static binding but the testability was an important factor.
I thought we can measure the execution time during the development and also run tests at the integration phases to decide if the performance overhead is acceptable.
In the past few days I received a critics that this approach will not work because late binding has an nondeterministict nature. This nondeterministict nature means even if I test it and I measure the execution times, later, in the production environment the execution time can be more only because of the late binding (because I used pure virtual functions).
As far as I know it is not possible (with exception of the cache misses and things like that). If you use interfaces it means you will have some additional layers of indirection and the compiler cannot optimize in some cases (inline functions for example), but that's it.
So my question is not the performance overhead but the variability of the performance. Can it vary between two execution?
I cannot find any article or benchmark about this topic. The found articles benchmark the constant performance difference between the static and dynamic binding but again it is not the question now.
If you know any openly accessible articles, webpages, books, source or anything that can help, please share with me.
Thank you!
Update: I would like to put the links and documents where I found the answer:
- How the virtual function call works: Parashift C++ FAQ
- Technical Report on C++ Performance, page 87: "If the static type of an object can be determined at compile-time, calling a virtual function may be no more expensive than calling a non-virtual member function. If the type must be dynamically determined at run-time, the overhead will typically be a fixed number of machine instructions (§5.3.3) for each call."
- Agnes Fog's Optimizing software in C++: An optimization guide for Windows, Linux and Mac platforms, page 53: "The time it takes to call a virtual member function is a few clock cycles more than it takes to call a non-virtual member function, provided that the function call statement always calls the same version of the virtual function."