diff options
| author | Martin Hořeňovský | 2024-07-22 10:08:59 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2024-07-22 10:54:16 +0200 |
| commit | 7af96bbb226930900b4b4902d7494c03bbba59c4 (patch) | |
| tree | 63ed510b1c5f1e549a1f45063d698c2c028471d8 /src | |
| parent | 22e6490325318d9a0c9820f7e1bb43c993adbbec (diff) | |
| download | catch3-7af96bbb226930900b4b4902d7494c03bbba59c4.tar.gz | |
Slight improvement to computing clock resolution in benchmarking
Diffstat (limited to 'src')
| -rw-r--r-- | src/catch2/benchmark/detail/catch_estimate_clock.hpp | 12 |
1 files changed, 7 insertions, 5 deletions
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 <typename Clock> std::vector<double> resolution(int k) { - std::vector<TimePoint<Clock>> times; - times.reserve(static_cast<size_t>(k + 1)); - for ( int i = 0; i < k + 1; ++i ) { - times.push_back( Clock::now() ); + const size_t points = static_cast<size_t>( k + 1 ); + // To avoid overhead from the branch inside vector::push_back, + // we allocate them all and then overwrite. + std::vector<TimePoint<Clock>> times(points); + for ( auto& time : times ) { + time = Clock::now(); } std::vector<double> deltas; deltas.reserve(static_cast<size_t>(k)); - for ( size_t idx = 1; idx < times.size(); ++idx ) { + for ( size_t idx = 1; idx < points; ++idx ) { deltas.push_back( static_cast<double>( ( times[idx] - times[idx - 1] ).count() ) ); } |