Search code examples
c++performancecross-platformvirtual-functionspimpl-idiom

C++ Pimpl vs Pure Virtual Interface Performance


I realize there are quite a few posts on this subject, but I am having trouble finding the answer to this exact question.

For function calls, which is faster, a pure-virtual interface or a pimpl?

At first glance, it seems to me that the pure-virtual interface would be faster, because the using the pimpl would cost two function calls instead of one...or would some kind of clever compiler trick take over in this case?

edit: I am trying to decide which of these I should use to abstract away the system-dependent portions of a few objects that may end up having to be spawned quite frequently, and in large numbers.

edit:
I suppose it's worth saying at this point, that the root of my problem was that I mistook the Abstract Factory design pattern for a method of making my code work on multiple platforms, when it's real purpose is for switching implementations for a given interface at runtime.


Solution

  • The two options are not equivalent, they should not be compared on performance as the focus is different. Even if they were equivalent, the performance difference would be minimal to unimportant in most situations. If you are in the rare case that you know that dispatch is being an issue, then you have the tools to measure the difference yourself.