Repository navigation
Expand file tree
/
Copy pathCmTimer.h
More file actions
83 lines (67 loc) · 1.8 KB
/
Copy pathCmTimer.h
File metadata and controls
83 lines (67 loc) · 1.8 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
#pragma once
class CmTimer
{
public:
CmTimer::CmTimer(CStr t):title(t) { is_started = false; start_clock = 0; cumulative_clock = 0; n_starts = 0; }
~CmTimer(){ if (is_started) printf("CmTimer '%s' is started and is being destroyed.\n", title.c_str()); }
inline void Start();
inline void Stop();
inline void Reset();
inline bool Report();
inline bool StopAndReport() { Stop(); return Report(); }
inline float TimeInSeconds();
private:
CStr title;
bool is_started;
clock_t start_clock;
clock_t cumulative_clock;
unsigned int n_starts;
};
/************************************************************************/
/* Implementations */
/************************************************************************/
void CmTimer::Start()
{
if (is_started){
printf("CmTimer '%s' is already started. Nothing done.\n", title.c_str());
return;
}
is_started = true;
n_starts++;
start_clock = clock();
}
void CmTimer::Stop()
{
if (!is_started){
printf("CmTimer '%s' is started. Nothing done\n", title.c_str());
return;
}
cumulative_clock += clock() - start_clock;
is_started = false;
}
void CmTimer::Reset()
{
if (is_started) {
printf("CmTimer '%s'is started during reset request.\n Only reset cumulative time.\n");
return;
}
cumulative_clock = 0;
}
bool CmTimer::Report()
{
if (is_started){
printf("CmTimer '%s' is started.\n Cannot provide a time report.", title.c_str());
return false;
}
float timeUsed = TimeInSeconds();
printf("[%s] CumuTime: %gs, #run: %d, AvgTime: %gs\n", title.c_str(), timeUsed, n_starts, timeUsed/n_starts);
return true;
}
float CmTimer::TimeInSeconds()
{
if (is_started){
printf("CmTimer '%s' is started. Nothing done\n", title.c_str());
return 0;
}
return float(cumulative_clock) / CLOCKS_PER_SEC;
}