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
1 changed files with 7 additions and 5 deletions

View File

@ -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() ) );
}