Slight improvement to computing clock resolution in benchmarking

This commit is contained in:
Martin Hořeňovský 2024-07-22 10:08:59 +02:00
parent 22e6490325
commit 7af96bbb22
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A

View File

@ -27,15 +27,17 @@ namespace Catch {
namespace Detail { namespace Detail {
template <typename Clock> template <typename Clock>
std::vector<double> resolution(int k) { std::vector<double> resolution(int k) {
std::vector<TimePoint<Clock>> times; const size_t points = static_cast<size_t>( k + 1 );
times.reserve(static_cast<size_t>(k + 1)); // To avoid overhead from the branch inside vector::push_back,
for ( int i = 0; i < k + 1; ++i ) { // we allocate them all and then overwrite.
times.push_back( Clock::now() ); std::vector<TimePoint<Clock>> times(points);
for ( auto& time : times ) {
time = Clock::now();
} }
std::vector<double> deltas; std::vector<double> deltas;
deltas.reserve(static_cast<size_t>(k)); 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>( deltas.push_back( static_cast<double>(
( times[idx] - times[idx - 1] ).count() ) ); ( times[idx] - times[idx - 1] ).count() ) );
} }