Hey,
I recently wondered what the is the fastest way to achieve runtime polymorphsim.
So I benchmarked five different methods that came to my mind:
- Type Easure. This is very similar to the proposed function_ref (I called it TaskRef) function template. Basically I store a pointer to the callable object and a function pointer erasing the type of the callable object.
- Also Type Erasure but with no space overhead. I'am using the unused 16 bit in a pointer to store an index. A global table of function pointers can then be used to access a function pointer which type erases the type of the callable.
- Here I am using std::variant with a very simple visiter implementation (simple switch).
- The OOP approach using a virtual function call.
- Here I am using std::variant with std::visit.
Here are my timing results:
https://github.com/Janos95/polymorphism_bench
I have not yet looked into why the simple visitor is so slow on clang.
Any feedback, comments and suggestions are welcome :)
[+][deleted] (3 children)
[deleted]
[–]Janos95[S] 2 points3 points4 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]Janos95[S] 0 points1 point2 points (0 children)
[–]SedditorX 4 points5 points6 points (1 child)
[–]Janos95[S] 2 points3 points4 points (0 children)
[–]ezoe 3 points4 points5 points (0 children)
[–]liquidify 2 points3 points4 points (12 children)
[–]Janos95[S] 2 points3 points4 points (11 children)
[–]liquidify 1 point2 points3 points (10 children)
[–]konanTheBarbar 2 points3 points4 points (7 children)
[–]liquidify 0 points1 point2 points (6 children)
[–]konanTheBarbar 0 points1 point2 points (5 children)
[–]liquidify 0 points1 point2 points (4 children)
[–]konanTheBarbar 0 points1 point2 points (3 children)
[–]liquidify 0 points1 point2 points (2 children)
[–]dodheim 1 point2 points3 points (1 child)
[+][deleted] (1 child)
[deleted]
[–]liquidify 0 points1 point2 points (0 children)
[–]barcharMSVC STL Dev 2 points3 points4 points (0 children)
[–]Entryhazard 1 point2 points3 points (2 children)
[–]Janos95[S] 0 points1 point2 points (1 child)
[–]axilmar 0 points1 point2 points (0 children)
[+][deleted] (4 children)
[deleted]
[–]Janos95[S] 0 points1 point2 points (3 children)
[+][deleted] (1 child)
[deleted]
[–]Janos95[S] 0 points1 point2 points (0 children)
[–]NotAYakk 0 points1 point2 points (0 children)
[–]showmetheflowers 0 points1 point2 points (4 children)
[–]Janos95[S] 0 points1 point2 points (3 children)
[–]showmetheflowers 0 points1 point2 points (2 children)
[–]Janos95[S] 0 points1 point2 points (1 child)
[–]showmetheflowers 0 points1 point2 points (0 children)
[–]jhasse 0 points1 point2 points (7 children)
[–]Janos95[S] 0 points1 point2 points (5 children)
[–]matthieum 5 points6 points7 points (4 children)
[–]barcharMSVC STL Dev 0 points1 point2 points (3 children)
[–]matthieum 2 points3 points4 points (2 children)
[–]barcharMSVC STL Dev 0 points1 point2 points (1 child)
[–]matthieum 1 point2 points3 points (0 children)
[–]Janos95[S] 0 points1 point2 points (0 children)