Skip to content

Commit

Permalink
Slight improvement to computing clock resolution in benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
horenmar committed Jul 22, 2024
1 parent 22e6490 commit 7af96bb
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/catch2/benchmark/detail/catch_estimate_clock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) );
}
Expand Down

0 comments on commit 7af96bb

Please sign in to comment.