C++ Utilities
Loading...
Searching...
No Matches
stopwatch.h
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: 2025 Nessan Fitzmaurice <nzznfitz+gh@icloud.com>
3// SPDX-License-Identifier: MIT
4
8
9#include <atomic>
10#include <chrono>
11#include <iostream>
12
13namespace utilities {
15template<typename Clock = std::chrono::high_resolution_clock>
16class stopwatch {
17public:
19 using clock_type = Clock;
20
22 explicit stopwatch(const std::string& str = "") : m_name(str) { reset(); }
23
25 std::string name() const { return m_name; }
26
28 std::string& name() { return m_name; }
29
31 constexpr void reset()
32 {
33 m_zero = clock_type::now();
34 m_split = 0;
35 m_prior = 0;
36 }
37
39 constexpr double elapsed() const
40 {
41 std::atomic_thread_fence(std::memory_order_relaxed);
42 auto t = clock_type::now();
43 std::atomic_thread_fence(std::memory_order_relaxed);
44 return std::chrono::duration<double>(t - m_zero).count();
45 }
46
48 constexpr double click()
49 {
50 auto tau = elapsed();
51 m_prior = m_split;
52 m_split = tau;
53 return m_split;
54 }
55
57 constexpr double split() const { return m_split; }
58
60 constexpr double lap() const { return m_split - m_prior; }
61
63 std::string to_string() const
64 {
65 auto tau = elapsed();
66 if (m_name.empty()) return std::format("{}s", tau);
67 return std::format("{}: {}s", m_name, tau);
68 }
69
70private:
71 using time_point = typename Clock::time_point;
72
73 std::string m_name; // Name of the stopwatch.
74 time_point m_zero; // The stopwatch measures time (in seconds) from this time point.
75 double m_split; // The total seconds to when the stopwatch was most recently clicked.
76 double m_prior; // The prior split.
77};
78
82template<typename Clock>
83inline std::ostream&
84operator<<(std::ostream& os, const stopwatch<Clock>& rhs)
85{
86 return os << rhs.to_string();
87}
88
90template<class Rep, class Period>
91constexpr double
92to_seconds(const std::chrono::duration<Rep, Period>& d)
93{
94 return std::chrono::duration<double>(d).count();
95}
96
99
102
105
106} // namespace utilities
See the Stopwatch page for all the details.
Definition stopwatch.h:16
constexpr void reset()
Set/reset the stopwatch's 'zero' point & clear any measured splits.
Definition stopwatch.h:31
constexpr double elapsed() const
Get the time that has passed from the zero point to now. Units are seconds.
Definition stopwatch.h:39
stopwatch(const std::string &str="")
A stopwatch can have a name to distinguish it from others you may have running.
Definition stopwatch.h:22
std::string to_string() const
Returns a string representation of the stopwatch's elapsed time in seconds.
Definition stopwatch.h:63
Clock clock_type
The underlying clock type.
Definition stopwatch.h:19
std::string name() const
Provides read-only access to the stopwatch's name.
Definition stopwatch.h:25
constexpr double lap() const
Returns the last 'lap' time in seconds (i.e. the time between prior 2 splits).
Definition stopwatch.h:60
constexpr double click()
Clicks the stopwatch to create a new 'split' and returns the elapsed time in seconds.
Definition stopwatch.h:48
constexpr double split() const
Returns the split as the time in seconds that elapsed from the zero point to the last click.
Definition stopwatch.h:57
std::string & name()
Provides read-write access to the stopwatch's name.
Definition stopwatch.h:28
The namespace for the utilities library.
Definition formatter.h:14
stopwatch< std::chrono::system_clock > system_stopwatch
A stopwatch that is uses the system clock.
Definition stopwatch.h:104
stopwatch< std::chrono::steady_clock > steady_stopwatch
A stopwatch that is guaranteed to be monotonic.
Definition stopwatch.h:101
stopwatch< std::chrono::high_resolution_clock > precise_stopwatch
Theoretically the most precise stopwatch – it might get put off by system reboots etc.
Definition stopwatch.h:98
constexpr double to_seconds(const std::chrono::duration< Rep, Period > &d)
A convenience function that converts a std::chrono::duration to a double number of seconds.
Definition stopwatch.h:92
std::ostream & operator<<(std::ostream &os, const stopwatch< Clock > &rhs)
The usual output operator.
Definition stopwatch.h:84