Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorMartin Hořeňovský2024-07-22 10:08:59 +0200
committerMartin Hořeňovský2024-07-22 10:54:16 +0200
commit7af96bbb226930900b4b4902d7494c03bbba59c4 (patch)
tree63ed510b1c5f1e549a1f45063d698c2c028471d8 /src
parent22e6490325318d9a0c9820f7e1bb43c993adbbec (diff)
downloadcatch3-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.hpp12
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() ) );
}