diff options
| author | Martin Hořeňovský | 2026-08-22 19:00:32 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2026-08-22 23:01:51 +0200 |
| commit | 69da66ee7370d7c249ba6f2a322a78dcdd623761 (patch) | |
| tree | a688adc0ac208a44dde27da6b2800b439a0f3b88 /src/catch2/benchmark/catch_benchmark.cpp | |
| parent | 45f9ea1e798c2e128dcba53602ccbddfe5676f6f (diff) | |
| download | catch3-69da66ee7370d7c249ba6f2a322a78dcdd623761.tar.gz | |
Don't instantiate the default implementation of benchmarks in every TU
The underlying benchmarking machinery is templated, so that it can use
different clock implementations (either for testing, or to support user
with platform-specific clocks). However, 99.99% of all users interact
with it using the `BENCHMARK` macro, which means that they use it with
`std::chrono::steady_clock`.
By adding outlined implementation of benchmarking helpers specialized
onto `std::chrono::steady_clock`, we save some amount of time per every
TU that uses benchmarks.
Diffstat (limited to 'src/catch2/benchmark/catch_benchmark.cpp')
| -rw-r--r-- | src/catch2/benchmark/catch_benchmark.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/catch2/benchmark/catch_benchmark.cpp b/src/catch2/benchmark/catch_benchmark.cpp new file mode 100644 index 00000000..5a34ed39 --- /dev/null +++ b/src/catch2/benchmark/catch_benchmark.cpp @@ -0,0 +1,54 @@ + +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 + +#include <catch2/benchmark/catch_benchmark.hpp> + +#include <algorithm> +#include <chrono> +#include <cmath> + +namespace Catch { + namespace Benchmark { + namespace Detail { + + Environment measure_environment_default() { + return Detail::measure_environment<default_clock>(); + } + + ExecutionPlan prepare_default( const IConfig& cfg, + Environment env, + BenchmarkFunction&& fun ) { + // This mirrors Benchmark::prepare<default_clock>(), but with the + // clock fixed so it is instantiated once here in the library. + auto min_time = env.clock_resolution.mean * Detail::minimum_ticks; + auto run_time = std::max( + min_time, + std::chrono::duration_cast<decltype( min_time )>( + cfg.benchmarkWarmupTime() ) ); + auto&& test = Detail::run_for_at_least<default_clock>( + std::chrono::duration_cast<IDuration>( run_time ), 1, fun ); + int new_iters = static_cast<int>( + std::ceil( min_time * test.iterations / test.elapsed ) ); + return { new_iters, + test.elapsed / test.iterations * new_iters * + cfg.benchmarkSamples(), + CATCH_MOVE( fun ), + std::chrono::duration_cast<FDuration>( + cfg.benchmarkWarmupTime() ), + Detail::warmup_iterations }; + } + + std::vector<FDuration> run_plan_default( ExecutionPlan const& plan, + const IConfig& cfg, + Environment env ) { + return plan.run<default_clock>( cfg, env ); + } + + } // namespace Detail + } // namespace Benchmark +} // namespace Catch |