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>;
33 void start() { start_time = std::chrono::steady_clock::now(); }
36 void stop() { stop_time = std::chrono::steady_clock::now(); }
44 void echo(std::string_view name)
const {
45 const auto elapsed = stop_time - start_time;
46 std::cout <<
"Stopwatch [" << name <<
"]\n"
47 <<
"Timer was active for : " << std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count() <<
" Milliseconds\n"
48 <<
"Timer was active for : " << std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count() <<
" Nanoseconds\n";
56 auto now = std::chrono::steady_clock::now();
57 return std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time).count();
61 std::chrono::steady_clock::time_point start_time, stop_time;
73 void start() { start_time = std::chrono::high_resolution_clock::now(); }
76 void stop() { stop_time = std::chrono::high_resolution_clock::now(); }
84 void echo(std::string_view name)
const {
85 const auto elapsed = stop_time - start_time;
86 std::cout <<
"Stopwatch [" << name <<
"]\n"
87 <<
"Timer was active for : " << std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count() <<
" Milliseconds\n"
88 <<
"Timer was active for : " << std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count() <<
" Nanoseconds\n";
96 auto now = std::chrono::high_resolution_clock::now();
97 return std::chrono::duration_cast<std::chrono::nanoseconds>(now - start_time).count();
101 std::chrono::high_resolution_clock::time_point start_time, stop_time;
149 clock_interface.start();
160 clock_interface.stop();
169 void Echo(std::string_view name)
const { clock_interface.echo(name); }
178 unsigned long TimePassed()
const {
return clock_interface.timePassed(); }
181 std::string_view time_name;
183 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.