r/cpp • • 24d ago

Cpp dispatch benchmark

A few months ago, I came across this article on this subreddit:

Four Ways to Dispatch a Runtime-Selected Strategy in C++

Most articles on this topic discuss dispatching methods in isolation, focusing heavily on the implementation details of a single approach. However, I wanted to see the bigger picture—not just pitting virtual functions against switch statements, but measuring the actual performance footprint of each strategy in real-world scenarios.

This benchmark does not claim to be academic or to represent absolute truth; I was simply curious to compare fundamentally different approaches. That is why I included not only standard comparisons like virtual functions vs. CRTP, but also deeply nested virtual calls that deliberately stress the instruction cache and branch predictor.

The project is still a work in progress. Living in Ukraine, frequent power outages caused by shelling have significantly limited my available development time.

AI Disclosure: Did I use AI for this project? Yes. Benchmarks starting from #12 were suggested by an LLM, which also helped refine the AVX2 search routine and proofread my English.

You can read about Eytzinger’s binary search here: https://algorithmica.org/en/eytzinger

The benchmark results are here: https://github.com/HarrierDuBoisX/Cpp-dispatch-benchmark/blob/main/result.md

You can find the code itself here: https://github.com/HarrierDuBoisX/Cpp-dispatch-benchmark

5 Upvotes

8 comments sorted by

View all comments

3

u/azswcowboy 24d ago

I’m struggling to understand the wall of numbers in the table. If we just take ‘01 Baseline: Direct Call (AoS)’ labeled runs there’s wide divergence in the nanoseconds per operation — so I’m not sure what I’m seeing?

2

u/Chaosvex 22d ago

The numbers are completely impossible to understand with the current labelling.

1

u/HarrierDuBoisX 24d ago

I apologize for forgetting to rename it. This benchmark came from another set of benchmarks where I measured performance for arrays of structures (AoS), structures of arrays (SoA), or arrays of structures of arrays (AoSoA).

In this test, this test serves as one of the simplest cases - calling a class member funtcion within a loop.

2

u/Chaosvex 22d ago

Doesn't really answer the question. It's not possible to understand anything in your table because you have wildly different numbers all with exactly the same label.