forked from alexander-nadel/intel_sat_solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeMeasure.h
194 lines (162 loc) · 4.24 KB
/
TimeMeasure.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright(C) 2021 Intel Corporation
// SPDX - License - Identifier: MIT
#pragma once
#include <chrono>
#include <ctime>
#include <limits>
struct CTimeOutException {};
struct CUserInterruptException {};
class CTimeMeasure
{
public:
CTimeMeasure(bool genericModeCpuTime = false, unsigned timeoutTestModuloFactor = 1) :
c_start(std::clock()),
t_start(std::chrono::high_resolution_clock::now()),
m_Timeout(TIME_INFINITY),
m_GenericModeCpuTime(genericModeCpuTime),
m_TimeoutTestModuloFactor(timeoutTestModuloFactor),
m_TimeoutTestCounter(0),
m_LatestResForTestCounter(0.)
{}
// Generic functions, which don't depend on the value of m_GenericModeCpuTime
inline double GetTimeout() const
{
return m_Timeout;
}
inline void SetModeWallTime()
{
m_GenericModeCpuTime = false;
}
inline void SetModeCpuTime()
{
m_GenericModeCpuTime = true;
}
inline bool IsTimeoutSet() const
{
return m_Timeout != TIME_INFINITY;
}
inline void SetTimeout(double timeout)
{
if (timeout > 0)
{
m_Timeout = timeout;
}
}
inline void Stop()
{
m_Timeout = 0.00000001;
}
inline void Reset()
{
c_start = std::clock();
t_start = std::chrono::high_resolution_clock::now();
}
// Generic functions, which depend on the value of m_GenericModeCpuTime
inline double TimePassedSinceStartOrReset()
{
return m_GenericModeCpuTime ? CpuTimePassedSinceStartOrReset() : WallTimePassedSinceStartOrReset();
}
inline bool IsTimeout()
{
return m_GenericModeCpuTime ? CpuIsTimeout() : WallIsTimeout();
}
inline void ThrowExceptionIfTimeoutPassed()
{
m_GenericModeCpuTime ? CpuTimeThrowExceptionIfTimeoutPassed() : WallTimeThrowExceptionIfTimeoutPassed();
}
inline double TimeLeftTillTimeout()
{
return m_GenericModeCpuTime ? CpuTimeLeftTillTimeout() : WallTimeLeftTillTimeout();
}
// Functionality per CPU time and per Wall time
inline double CpuTimePassedSinceStartOrResetConst() const
{
return (std::clock() - c_start) / CLOCKS_PER_SEC;
}
double CpuTimePassedSinceStartOrReset()
{
if (m_TimeoutTestModuloFactor == 1)
{
return CpuTimePassedSinceStartOrResetConst();
}
else
{
if (m_TimeoutTestCounter % m_TimeoutTestModuloFactor == 0)
{
++m_TimeoutTestCounter;
return m_LatestResForTestCounter = (std::clock() - c_start) / CLOCKS_PER_SEC;
}
else
{
++m_TimeoutTestCounter;
return m_LatestResForTestCounter;
}
}
}
inline double WallTimePassedSinceStartOrResetConst() const
{
const auto t_end = std::chrono::high_resolution_clock::now();
return std::chrono::duration<double>(t_end - t_start).count();
}
double WallTimePassedSinceStartOrReset()
{
if (m_TimeoutTestModuloFactor == 1)
{
return WallTimePassedSinceStartOrResetConst();
}
else
{
if (m_TimeoutTestCounter % m_TimeoutTestModuloFactor == 0)
{
++m_TimeoutTestCounter;
auto t_end = std::chrono::high_resolution_clock::now();
return m_LatestResForTestCounter = std::chrono::duration<double>(t_end - t_start).count();
}
else
{
++m_TimeoutTestCounter;
return m_LatestResForTestCounter;
}
}
}
inline bool CpuIsTimeout()
{
return m_Timeout - CpuTimePassedSinceStartOrReset() <= 0;
}
inline bool WallIsTimeout()
{
return m_Timeout < TIME_INFINITY && m_Timeout - WallTimePassedSinceStartOrReset() <= 0;
}
inline void CpuTimeThrowExceptionIfTimeoutPassed()
{
if (CpuIsTimeout())
{
throw CTimeOutException();
}
}
inline void WallTimeThrowExceptionIfTimeoutPassed()
{
if (WallIsTimeout())
{
throw CTimeOutException();
}
}
inline double WallTimeLeftTillTimeout()
{
return (m_Timeout == TIME_INFINITY) ? TIME_INFINITY : m_Timeout - WallTimePassedSinceStartOrReset();
}
inline double CpuTimeLeftTillTimeout()
{
return (m_Timeout == TIME_INFINITY) ? TIME_INFINITY : m_Timeout - CpuTimePassedSinceStartOrReset();
}
constexpr static double TIME_INFINITY = (std::numeric_limits<double>::max)();
inline void SetTestModuloFactor(unsigned timeoutTestModuloFactor) { m_TimeoutTestModuloFactor = timeoutTestModuloFactor; }
private:
std::clock_t c_start;
std::chrono::high_resolution_clock::time_point t_start;
double m_Timeout;
bool m_GenericModeCpuTime;
unsigned m_TimeoutTestModuloFactor;
unsigned m_TimeoutTestCounter;
double m_LatestResForTestCounter;
};