diff --git a/src/catch2/benchmark/detail/catch_estimate_clock.hpp b/src/catch2/benchmark/detail/catch_estimate_clock.hpp index 8e355279..6da24ce5 100644 --- a/src/catch2/benchmark/detail/catch_estimate_clock.hpp +++ b/src/catch2/benchmark/detail/catch_estimate_clock.hpp @@ -27,15 +27,17 @@ namespace Catch { namespace Detail { template std::vector resolution(int k) { - std::vector> times; - times.reserve(static_cast(k + 1)); - for ( int i = 0; i < k + 1; ++i ) { - times.push_back( Clock::now() ); + const size_t points = static_cast( k + 1 ); + // To avoid overhead from the branch inside vector::push_back, + // we allocate them all and then overwrite. + std::vector> times(points); + for ( auto& time : times ) { + time = Clock::now(); } std::vector deltas; deltas.reserve(static_cast(k)); - for ( size_t idx = 1; idx < times.size(); ++idx ) { + for ( size_t idx = 1; idx < points; ++idx ) { deltas.push_back( static_cast( ( times[idx] - times[idx - 1] ).count() ) ); }