Hi, I am writing a simple benchmark wrapper that can be run with multiple possible functions (that all fit the same signature). I am trying to use template parameters instead of a function pointer or std::function here, because in general this method will allow the compiler to inline the function given rather than calling it through the pointer. My wrapper looks like this.
template <typename Dispatch>
void runBenchmark(const Context<Dispatch>& ctx, const auto&& benchmark, const std::string_view name, bool bda) {
static constexpr size_t BENCHMARK_ITERATIONS = 1000;
const auto buffers = createBuffers(ctx, bda);
std::array<double, BENCHMARK_ITERATIONS> times{};
for (size_t i = 0; i < BENCHMARK_ITERATIONS; i++) {
times[i] = static_cast<double>(benchmark(ctx, buffers).count()) / 1e6;
}
const double mean = std::reduce(times.begin(), times.end(), 0.0) / BENCHMARK_ITERATIONS;
const double stddev = std::accumulate(times.begin(), times.end(), 0.0, [mean](double sum, double x) { return sum + (x - mean) * (x - mean); }) / BENCHMARK_ITERATIONS;
std::println("{} mean: {}ms", name, mean);
std::println("{} stddev: {}ms", name, stddev);
}