-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.h
More file actions
163 lines (140 loc) · 4.53 KB
/
timer.h
File metadata and controls
163 lines (140 loc) · 4.53 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef TIMER_H
#define TIMER_H
#include <chrono>
#include <mutex>
#include <vector>
class [[nodiscard]] ThreadTimer; // needed to add it as friend in timer class
// ============================================================================================= //
// Begin class timer //
/**
* @brief A helper class to measure execution time for benchmarking purposes.
* * The timer can be set back to ZERO thanks to the reboot() function
*/
class [[nodiscard]] Timer
{
public:
friend class ThreadTimer;
/**
* @brief Start (or restart) measuring time.
*/
void start()
{
start_time = std::chrono::steady_clock::now();
}
/**
* @brief Stop measuring time and store the elapsed time since start().
*/
void stop()
{
elapsed_time += std::chrono::steady_clock::now() - start_time;
}
/**
* @brief reboot the timer to zero
*/
void reset()
{
elapsed_time = std::chrono::duration<double>::zero();
}
/**
* @brief Get the number of milliseconds that have elapsed between start() and stop().
*
* @return The number of milliseconds.
*/
[[nodiscard]] std::chrono::milliseconds::rep ms() const
{
return (std::chrono::duration_cast<std::chrono::milliseconds>(elapsed_time)).count();
}
private:
/**
* @brief The time point when measuring started.
*/
std::chrono::time_point<std::chrono::steady_clock> start_time = std::chrono::steady_clock::now();
/**
* @brief The duration that has elapsed between start() and stop().
*/
std::chrono::duration<double> elapsed_time = std::chrono::duration<double>::zero();
};
// End class Timer //
// ============================================================================================= //
// ============================================================================================= //
// Begin class ThreadTimer //
/**
* @brief A helper class to measure execution time for benchmarking purposes.
* Compared to the "basic" timer, the thread timer can be used INSIDE threads to compute the cumulative time spent in a portion of code
* (for exemple, in a parallèle for loop)
*
* To use it :
* - declare the timer advanced as a global variable (outside any function)
* - when started, the timer return a token
* - feed this token to the stop method
* - enjoy the result of your benchmark !
*
* The timer can be set back to ZERO thanks to the reboot() function
*/
class [[nodiscard]] ThreadTimer
{
public:
friend Timer;
/**
* @brief Start measuring time.
* return a token for the stop() method
*/
int start()
{
this->mutex.lock();
Timer my_timer;
this->timer_list.emplace_back(my_timer);
int token = this->list_size;
this->list_size++;
this->timer_list.back().start();
this->mutex.unlock();
return token;
}
/**
* @brief Stop measuring time and add the measured time to the total.
* @arg start_token : the token given by the start() method
*/
void stop(int start_token)
{
this->mutex.lock();
timer_list[start_token].stop();
this->mutex.unlock();
}
/**
* @brief reboot the timer to zero
*/
void reset()
{
this->timer_list.clear();
this->list_size = 0;
}
/**
* @brief Get the number of milliseconds that have elapsed between start() and stop().
*
* @return The number of milliseconds.
*/
[[nodiscard]] int ms()
{
std::chrono::duration<double> duration_total = std::chrono::duration<double>::zero();
for( timer timer : timer_list){
duration_total += timer.elapsed_time;
}
return (std::chrono::duration_cast<std::chrono::milliseconds>(duration_total)).count();
}
private:
/**
* brief: the list of timer
*/
std::vector<Timer> timer_list;
/**
* @brief the timer_list size
*/
size_t list_size = 0;
/**
* @brief the mutex of the timer (used to protect the vector)
*/
std::mutex mutex;
};
// Begin class threadTimer //
// ============================================================================================= //
#endif