Flow
Documentation for the Flow C++ Library
Loading...
Searching...
No Matches
flow_timer.h
Go to the documentation of this file.
1#pragma once
2#include <chrono>
3#include <format>
4#include <string>
5#include <vector>
6
7namespace flow {
8
10 template <typename ClockType = std::chrono::steady_clock>
11 class Timer {
12 using TimePoint = typename ClockType::time_point;
14 std::vector<TimePoint> timepoints_{};
15
16 public:
19 explicit constexpr Timer(std::size_t reserveSize = 8) {
20 timepoints_.reserve(reserveSize);
21 }
22
25 constexpr std::size_t size() const {
26 return timepoints_.size();
27 }
28
30 void reset() {
31 timepoints_.clear();
32 begin_ = ClockType::now();
33 }
34
36 void record() {
37 timepoints_.push_back(ClockType::now());
38 }
39
42 std::string toString() const {
43 std::string str = std::format("Total record entries: {}\n", timepoints_.size());
44 std::size_t i = 0;
45 for (const auto& timepoint : timepoints_) {
46 auto dur = timepoint - begin_;
47 str += std::format("\t[{}] {}, {}, {}\n",
48 i,
49 std::chrono::duration_cast<std::chrono::seconds>(dur),
50 std::chrono::duration_cast<std::chrono::milliseconds>(dur),
51 std::chrono::duration_cast<std::chrono::microseconds>(dur));
52 ++i;
53 }
54 return str;
55 }
56 };
57
58}
constexpr std::size_t size() const
Returns the number of recorded time points.
Definition flow_timer.h:25
void record()
Records the current time point relative to the last reset.
Definition flow_timer.h:36
std::string toString() const
Formats and returns a string showing all recorded durations since reset.
Definition flow_timer.h:42
std::vector< TimePoint > timepoints_
Definition flow_timer.h:14
void reset()
Clears all recorded time points and resets the starting time.
Definition flow_timer.h:30
constexpr Timer(std::size_t reserveSize=8)
Constructs a Timer and reserves space for time points.
Definition flow_timer.h:19
typename ClockType::time_point TimePoint
Definition flow_timer.h:12
TimePoint begin_
Definition flow_timer.h:13