-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstopwatch.h
41 lines (32 loc) · 837 Bytes
/
stopwatch.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#pragma once
#include <chrono>
#include <cstdio>
class Stopwatch
{
public:
void start ()
{
begin = std::chrono::steady_clock::now();
is_running = true;
}
void stop ()
{
end = std::chrono::steady_clock::now();
face = end - begin;
is_running = false;
}
void click ()
{
if (is_running) stop();
else start();
}
void read () const
{
std::printf("Elapsed time: %.2f seconds\n", face.count());
}
private:
std::chrono::time_point<std::chrono::steady_clock> begin = {};
std::chrono::time_point<std::chrono::steady_clock> end = {};
std::chrono::duration<double> face = {};
bool is_running = false;
}; // class Stopwatch