Flow
Documentation for the Flow C++ Library
Loading...
Searching...
No Matches
flow::Timer< ClockType > Class Template Reference

A simple timer to record timelapses. Uses steady_clock by default. More...

#include <flow_timer.h>

Public Member Functions

constexpr Timer (std::size_t reserveSize=8)
 Constructs a Timer and reserves space for time points.
constexpr std::size_t size () const
 Returns the number of recorded time points.
void reset ()
 Clears all recorded time points and resets the starting time.
void record ()
 Records the current time point relative to the last reset.
std::string toString () const
 Formats and returns a string showing all recorded durations since reset.

Private Types

using TimePoint = typename ClockType::time_point

Private Attributes

TimePoint begin_ {}
std::vector< TimePointtimepoints_ {}

Detailed Description

template<typename ClockType = std::chrono::steady_clock>
class flow::Timer< ClockType >

A simple timer to record timelapses. Uses steady_clock by default.

Definition at line 11 of file flow_timer.h.

Member Typedef Documentation

◆ TimePoint

template<typename ClockType = std::chrono::steady_clock>
using flow::Timer< ClockType >::TimePoint = typename ClockType::time_point
private

Definition at line 12 of file flow_timer.h.

Constructor & Destructor Documentation

◆ Timer()

template<typename ClockType = std::chrono::steady_clock>
flow::Timer< ClockType >::Timer ( std::size_t reserveSize = 8)
inlineexplicitconstexpr

Constructs a Timer and reserves space for time points.

Parameters
reserveSizeNumber of time points to reserve space for.

Definition at line 19 of file flow_timer.h.

19 {
20 timepoints_.reserve(reserveSize);
21 }
A simple timer to record timelapses. Uses steady_clock by default.
Definition flow_timer.h:11
std::vector< TimePoint > timepoints_
Definition flow_timer.h:14

References timepoints_.

Member Function Documentation

◆ record()

template<typename ClockType = std::chrono::steady_clock>
void flow::Timer< ClockType >::record ( )
inline

Records the current time point relative to the last reset.

Definition at line 36 of file flow_timer.h.

36 {
37 timepoints_.push_back(ClockType::now());
38 }

References timepoints_.

◆ reset()

template<typename ClockType = std::chrono::steady_clock>
void flow::Timer< ClockType >::reset ( )
inline

Clears all recorded time points and resets the starting time.

Definition at line 30 of file flow_timer.h.

30 {
31 timepoints_.clear();
33 }
TimePoint begin_
Definition flow_timer.h:13

References begin_, and timepoints_.

◆ size()

template<typename ClockType = std::chrono::steady_clock>
std::size_t flow::Timer< ClockType >::size ( ) const
inlineconstexpr

Returns the number of recorded time points.

Returns
Count of recorded time points.

Definition at line 25 of file flow_timer.h.

25 {
26 return timepoints_.size();
27 }

References timepoints_.

◆ toString()

template<typename ClockType = std::chrono::steady_clock>
std::string flow::Timer< ClockType >::toString ( ) const
inline

Formats and returns a string showing all recorded durations since reset.

Returns
Formatted string of all recorded time intervals.

Definition at line 42 of file flow_timer.h.

42 {
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,
52 ++i;
53 }
54 return str;
55 }

References begin_, and timepoints_.

Member Data Documentation

◆ begin_

template<typename ClockType = std::chrono::steady_clock>
TimePoint flow::Timer< ClockType >::begin_ {}
private

Definition at line 13 of file flow_timer.h.

13{};

Referenced by reset(), and toString().

◆ timepoints_

template<typename ClockType = std::chrono::steady_clock>
std::vector<TimePoint> flow::Timer< ClockType >::timepoints_ {}
private

Definition at line 14 of file flow_timer.h.

14{};

Referenced by record(), reset(), size(), Timer(), and toString().


The documentation for this class was generated from the following file: