16concept ClockPolicy =
requires(T clock, std::string_view name) {
17 { clock.start() } -> std::same_as<void>;
18 { clock.stop() } -> std::same_as<void>;
19 { clock.echo(name) } -> std::same_as<void>;
20 { clock.timePassed() } -> std::convertible_to<unsigned long>;
34 start_time = std::chrono::steady_clock::now();
39 stop_time = std::chrono::steady_clock::now();
48 void echo(std::string_view name)
const {
49 const auto elapsed = stop_time - start_time;
50 std::cout <<
"Stopwatch [" << name <<
"]\n"
51 <<
"Timer was active for : "
52 << std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count()
54 <<
"Timer was active for : "
55 << std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count()
64 auto now = std::chrono::steady_clock::now();
65 return std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time).count();
69 std::chrono::steady_clock::time_point start_time, stop_time;
82 start_time = std::chrono::high_resolution_clock::now();
87 stop_time = std::chrono::high_resolution_clock::now();
96 void echo(std::string_view name)
const {
97 const auto elapsed = stop_time - start_time;
98 std::cout <<
"Stopwatch [" << name <<
"]\n"
99 <<
"Timer was active for : "
100 << std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count()
102 <<
"Timer was active for : "
103 << std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count()
112 auto now = std::chrono::high_resolution_clock::now();
113 return std::chrono::duration_cast<std::chrono::nanoseconds>(now - start_time).count();
117 std::chrono::high_resolution_clock::time_point start_time, stop_time;
132template <ClockPolicy T>
139 explicit StopWatch(std::string_view name) : time_name(name) {
168 clock_interface.start();
179 clock_interface.stop();
188 void Echo(std::string_view name)
const {
189 clock_interface.echo(name);
200 return clock_interface.timePassed();
204 std::string_view time_name;
206 bool m_stopped =
false;
Stopwatch policy backed by std::chrono::high_resolution_clock.
void stop()
Record the end of a timed interval.
unsigned long timePassed() const
Return the time elapsed since the most recent start().
void echo(std::string_view name) const
Print the recorded interval in milliseconds and nanoseconds.
void start()
Record the beginning of a timed interval.
Stopwatch policy backed by std::chrono::steady_clock.
void stop()
Record the end of a timed interval.
void echo(std::string_view name) const
Print the recorded interval in milliseconds and nanoseconds.
void start()
Record the beginning of a timed interval.
unsigned long timePassed() const
Return the time elapsed since the most recent start().
StopWatch & operator=(StopWatch &&)=delete
Stopwatches cannot be move-assigned.
StopWatch & operator=(const StopWatch &)=delete
Stopwatches cannot be copy-assigned.
StopWatch(StopWatch &&)=delete
Stopwatches cannot be moved.
void Stop()
Stop the current interval and print its duration.
StopWatch(const StopWatch &)=delete
Stopwatches cannot be copied.
StopWatch(std::string_view name)
Construct and immediately start a stopwatch.
void Echo(std::string_view name) const
Print the interval recorded by the clock policy.
~StopWatch()
Stop and print the timer if it is still running.
unsigned long TimePassed() const
Query the time elapsed since the most recent Start().
void Start(std::string_view name)
Start a new timed interval.
Defines the interface required by a stopwatch clock policy.