Skip to content

Commit 93829e3

Browse files
committed
Slight improvement to computing clock resolution in benchmarking
1 parent f1196e2 commit 93829e3

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/catch2/benchmark/detail/catch_estimate_clock.hpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@ namespace Catch {
2727
namespace Detail {
2828
template <typename Clock>
2929
std::vector<double> resolution(int k) {
30-
std::vector<TimePoint<Clock>> times;
31-
times.reserve(static_cast<size_t>(k + 1));
32-
for ( int i = 0; i < k + 1; ++i ) {
33-
times.push_back( Clock::now() );
30+
const size_t points = static_cast<size_t>( k + 1 );
31+
// To avoid overhead from the branch inside vector::push_back,
32+
// we allocate them all and then overwrite.
33+
std::vector<TimePoint<Clock>> times(points);
34+
for ( auto& time : times ) {
35+
time = Clock::now();
3436
}
3537

3638
std::vector<double> deltas;
3739
deltas.reserve(static_cast<size_t>(k));
38-
for ( size_t idx = 1; idx < times.size(); ++idx ) {
40+
for ( size_t idx = 1; idx < points; ++idx ) {
3941
deltas.push_back( static_cast<double>(
4042
( times[idx] - times[idx - 1] ).count() ) );
4143
}

0 commit comments

Comments
 (0)