cpp-stat-bench 0.24.0
Benchmark library with statistics for C++.
Loading...
Searching...
No Matches
stop_watch.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <cstddef>
23#include <stdexcept>
24#include <vector>
25
28
29namespace stat_bench {
30namespace clock {
31
35class StopWatch {
36public:
40 StopWatch() = default;
41
42 StopWatch(const StopWatch&) = delete;
43 StopWatch(StopWatch&&) = delete;
44 auto operator=(const StopWatch&) -> StopWatch& = delete;
45 auto operator=(StopWatch&&) -> StopWatch& = delete;
46
50 ~StopWatch() = default;
51
59 void start(std::size_t num_laps) {
60 time_points_.clear();
61 time_points_.reserve(num_laps + 1);
62 lap();
63 }
64
68 void lap() { time_points_.push_back(MonotoneTimePoint::now()); }
69
75 [[nodiscard]] auto calc_durations() const {
76 if (time_points_.size() < 2) {
77 throw std::runtime_error("Too few number of time points.");
78 }
79 std::vector<Duration> durations;
80 durations.reserve(time_points_.size() - 1);
81 for (std::size_t i = 0; i < time_points_.size() - 1; ++i) {
82 durations.push_back(time_points_[i + 1] - time_points_[i]);
83 }
84 return durations;
85 }
86
87private:
89 std::vector<MonotoneTimePoint> time_points_{};
90};
91
92} // namespace clock
93} // namespace stat_bench
~StopWatch()=default
Destructor.
StopWatch()=default
Constructor.
auto calc_durations() const
Calculate measured durations.
Definition stop_watch.h:75
void lap()
Measure a time point for a lap.
Definition stop_watch.h:68
void start(std::size_t num_laps)
Start measurement.
Definition stop_watch.h:59
Definition of Duration class.
Definition of classes of monotone clocks.
Namespace of clocks for benchmarks.
Definition duration.h:23
Namespace of stat_bench source codes.