r/cpp • • May 26 '22

Dynamic polymorphism performance

https://thecppdev.com/2022/05/26/dynamic-polymorphism-performance/?amp=1
2 Upvotes

15 comments sorted by

View all comments

30

u/SirB May 26 '22

Summary: An artificial test on indirection shows that indirection is slower than no indirection by a significant amount. A bit pointless imo. It would be more interesting to talk about maintainability, flexibility and overhead when avoiding this kind of indirection inside hot loops but allowing it outside, in 'management' code.

10

u/pandorafalters May 26 '22

It gets far more interesting with actual code in your classes.

I have a real-world example of whole-program benchmarking showing an improvement in performance by using a pointer to an interface type (with the concrete classes further using PIMPL) rather than stuffing everything directly in the top-level class. So multiple indirections were faster than no indirection at all. (Slightly. Averaging ~1.4%, but always positive over many runs ranging from a few seconds to several months.)

Microbenchmarks should never be your only tool.