#pragma once #include #include #include #include #include namespace Void::Bench { using Clock = std::chrono::steady_clock; using Duration = std::chrono::nanoseconds; // ── BenchResult ─────────────────────────────────────────────────────────────── struct BenchResult { std::string name; std::size_t iterations; Duration total; Duration min_ns; Duration max_ns; [[nodiscard]] double mean_ns() const noexcept { return iterations > 0 ? static_cast(total.count()) / static_cast(iterations) : 0.0; } [[nodiscard]] double ops_per_sec() const noexcept { const double total_s = std::chrono::duration(total).count(); return total_s > 0.0 ? static_cast(iterations) / total_s : 0.0; } }; // ── run ─────────────────────────────────────────────────────────────────────── // Run fn for `iterations` iterations, discarding the first `warmup` runs. // The body is not inlined / optimized away — use DoNotOptimize below. [[nodiscard]] inline BenchResult run(std::string name, std::size_t iterations, std::function fn, std::size_t warmup = 3) { for (std::size_t i = 0; i < warmup; ++i) fn(); Duration total{0}; Duration mn = Duration::max(); Duration mx = Duration::min(); for (std::size_t i = 0; i < iterations; ++i) { const auto t0 = Clock::now(); fn(); const auto dt = Clock::now() - t0; const auto ns = std::chrono::duration_cast(dt); total += ns; if (ns < mn) mn = ns; if (ns > mx) mx = ns; } return {std::move(name), iterations, total, mn, mx}; } // ── run_for ─────────────────────────────────────────────────────────────────── // Run fn as many times as possible within `budget`, return result. template [[nodiscard]] inline BenchResult run_for(std::string name, std::chrono::duration budget, std::function fn, std::size_t warmup = 3) { for (std::size_t i = 0; i < warmup; ++i) fn(); Duration total{0}; Duration mn = Duration::max(); Duration mx = Duration::min(); std::size_t count = 0; const auto deadline = Clock::now() + budget; while (Clock::now() < deadline) { const auto t0 = Clock::now(); fn(); const auto ns = std::chrono::duration_cast(Clock::now() - t0); total += ns; if (ns < mn) mn = ns; if (ns > mx) mx = ns; ++count; } if (mn == Duration::max()) mn = Duration{0}; if (mx == Duration::min()) mx = Duration{0}; return {std::move(name), count, total, mn, mx}; } // ── DoNotOptimize ───────────────────────────────────────────────────────────── // Prevent the compiler from optimizing away a value. template inline void do_not_optimize(T& val) noexcept { #if defined(__GNUC__) || defined(__clang__) // A memory operand works for every object type and remains accepted by // GCC 16, which rejects the old mixed "+r,m" alternative constraint for // some scalar instantiations. asm volatile("" : "+m"(val) : : "memory"); #else volatile T* p = &val; (void)*p; #endif } template inline void do_not_optimize(const T& val) noexcept { #if defined(__GNUC__) || defined(__clang__) asm volatile("" : : "m"(val) : "memory"); #else volatile const T* p = &val; (void)*p; #endif } // ── Suite ───────────────────────────────────────────────────────────────────── // Collect multiple benchmark results. class Suite { public: void add(BenchResult r) { results_.push_back(std::move(r)); } [[nodiscard]] const std::vector& results() const noexcept { return results_; } [[nodiscard]] std::size_t size() const noexcept { return results_.size(); } private: std::vector results_; }; } // namespace Void::Bench