forked from manzali/lseb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.h
More file actions
61 lines (52 loc) · 1.71 KB
/
Copy pathtimer.h
File metadata and controls
61 lines (52 loc) · 1.71 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef COMMON_TIMER_H
#define COMMON_TIMER_H
#include <chrono>
#include <cassert>
namespace lseb {
class Timer {
std::chrono::high_resolution_clock::time_point m_begin_time;
std::chrono::high_resolution_clock::time_point m_start_time;
std::chrono::high_resolution_clock::duration m_active_time;
bool m_paused;
public:
Timer()
:
m_begin_time(std::chrono::high_resolution_clock::now()),
m_active_time(std::chrono::high_resolution_clock::duration::zero()),
m_paused(true) {
}
void start() {
assert(m_paused == true && "It's already started");
m_start_time = std::chrono::high_resolution_clock::now();
m_paused = false;
}
void pause() {
assert(m_paused == false && "It's already paused");
m_active_time += std::chrono::high_resolution_clock::duration(
std::chrono::high_resolution_clock::now() - m_start_time);
m_paused = true;
}
std::chrono::high_resolution_clock::duration active_time() {
if (m_paused) {
return m_active_time;
}
return m_active_time + std::chrono::high_resolution_clock::duration(
std::chrono::high_resolution_clock::now() - m_start_time);
}
std::chrono::high_resolution_clock::duration total_time() {
return std::chrono::high_resolution_clock::duration(
std::chrono::high_resolution_clock::now() - m_begin_time);
}
double rate() {
return active_time() * 100. / total_time();
}
void reset() {
m_begin_time = std::chrono::high_resolution_clock::now();
m_active_time = std::chrono::high_resolution_clock::duration::zero();
m_paused = true;
}
Timer(const Timer&) = delete; // disable copying
Timer& operator=(const Timer&) = delete; // disable assignment
};
}
#endif