OdinAI

- Main Page
- Namespaces
- Classes
- Files
- Examples

- File List

All Classes Namespaces Functions Variables
Timer.h
1 #ifndef ODINAI_TIMER_H_
2 #define ODINAI_TIMER_H_
3
4 #ifdef _WIN32
5 #include <Windows.h>
6 #else
7 #include <ctime>
8 #include <chrono>
9 #include <ratio>
10 using namespace std::chrono;
11 #endif
12
13 #include "MathUtil.h"
14
18 namespace OdinAI
19 {
20
21
22 class Timer
23 {
24 public:
25 Timer(){ Reset(); }
26
30 static bool Init()
31 {
32 #ifdef _WIN32
33 if (!QueryPerformanceFrequency(&m_ticksPerSecond))
34 {
35 return false;
36 }
37
38 m_realFreq = 1.0 / (double)m_ticksPerSecond.QuadPart;
39 #endif
40 GlobalReset();
41
42 return true;
43 }
44
48 static void GlobalReset()
49 {
50 #ifdef _WIN32
51 QueryPerformanceCounter(&m_gStartTime);
52 m_gElapsedTime = m_gStartTime;
53 #else
54 m_gStartTime = high_resolution_clock::now();
55 m_gElapsedTime = m_gStartTime;
56 #endif
57 }
58
63 template<typename T>
64 static T GetGlobalElapsedTime()
65 {
66 #ifdef _WIN32
67 LARGE_INTEGER currentTime;
68 QueryPerformanceCounter(&currentTime);
69
70 T sec = (T)(currentTime.QuadPart - m_gElapsedTime.QuadPart) * m_realFreq;
71 m_gElapsedTime = currentTime;
72 return sec;
73 #else
74 TimePoint currentTime = high_resolution_clock::now();
75 duration<T> time_span = duration_cast< duration<T> >(currentTime - m_gElapsedTime);
76 m_gElapsedTime = currentTime;
77 return time_span.count();
78 #endif
79 }
80
84 template<typename T>
85 static T GetGlobalElapsedMillisec()
86 {
87 #ifdef _WIN32
88 LARGE_INTEGER currentTime;
89 QueryPerformanceCounter(&currentTime);
90 double milliSec = (double)(currentTime.QuadPart - m_gElapsedTime.QuadPart) / (double)m_ticksPerSecond.QuadPart;
91
92 m_gElapsedTime = currentTime;
93
94 return (T)(milliSec * 1000);
95 #else
96 TimePoint currentTime = high_resolution_clock::now();
97 milliseconds time_span = duration_cast< milliseconds >(currentTime - m_gElapsedTime);
98 m_gElapsedTime = currentTime;
99 return (T)time_span.count();
100 #endif
101 }
102
106 template<typename T>
107 static T GetGlobalMillisec()
108 {
109 #ifdef _WIN32
110 LARGE_INTEGER currentTime;
111 QueryPerformanceCounter(&currentTime);
112 double milliSec = (double)(currentTime.QuadPart - m_gStartTime.QuadPart) / (double)m_ticksPerSecond.QuadPart;
113
114 return (T)(milliSec * 1000);
115 #else
116 TimePoint currentTime = high_resolution_clock::now();
117 milliseconds time_span = duration_cast< milliseconds >(currentTime - m_gStartTime);
118
119 return (T)time_span.count();
120 #endif
121 }
122
126 void Reset()
127 {
128 #ifdef _WIN32
129 QueryPerformanceCounter(&m_startTime);
130 m_elapsedTime = m_startTime;
131 #else
132 m_startTime = high_resolution_clock::now();
133 m_elapsedTime = m_startTime;
134 #endif
135 }
136
141 template<typename T>
142 T GetElapsedTime()
143 {
144 #ifdef _WIN32
145 LARGE_INTEGER currentTime;
146 QueryPerformanceCounter(&currentTime);
147
148 T sec = (T)(currentTime.QuadPart - m_elapsedTime.QuadPart) * m_realFreq;
149 m_elapsedTime = currentTime;
150 return sec;
151 #else
152 TimePoint currentTime = high_resolution_clock::now();
153 duration<T> time_span = duration_cast< duration<T> >(currentTime - m_elapsedTime);
154 m_elapsedTime = currentTime;
155 return time_span.count();
156 #endif
157 }
158
162 template<typename T>
163 T GetElapsedMillisec()
164 {
165 #ifdef _WIN32
166 LARGE_INTEGER currentTime;
167 QueryPerformanceCounter(&currentTime);
168 double milliSec = (double)(currentTime.QuadPart - m_elapsedTime.QuadPart) / (double)m_ticksPerSecond.QuadPart;
169
170 m_elapsedTime = currentTime;
171
172 return (T)(milliSec * 1000);
173 #else
174 TimePoint currentTime = high_resolution_clock::now();
175 milliseconds time_span = duration_cast<milliseconds>(currentTime - m_elapsedTime);
176 m_elapsedTime = currentTime;
177 return (T)time_span.count();
178 #endif
179 }
180
184 template<typename T>
185 T GetMillisec()
186 {
187 #ifdef _WIN32
188 LARGE_INTEGER currentTime;
189 QueryPerformanceCounter(&currentTime);
190 double milliSec = (double)(currentTime.QuadPart - m_startTime.QuadPart) / (double)m_ticksPerSecond.QuadPart;
191
192 return (T)(milliSec * 1000);
193 #else
194 TimePoint currentTime = high_resolution_clock::now();
195 milliseconds time_span = duration_cast<milliseconds>(currentTime - m_startTime);
196 return (T)time_span.count();
197 #endif
198 }
199
203 template<typename T>
204 T GetTime()
205 {
206 #ifdef _WIN32
207 LARGE_INTEGER currentTime;
208 QueryPerformanceCounter(&currentTime);
209 T secs = (T)(currentTime.QuadPart - m_startTime.QuadPart) / (T)m_ticksPerSecond.QuadPart;
210
211 return secs;
212 #else
213 TimePoint currentTime = high_resolution_clock::now();
214 duration<T> time_span = duration_cast< duration<T> >(currentTime - m_startTime);
215 return time_span.count();
216 #endif
217 }
218
219 #ifdef _WIN32
220 static LARGE_INTEGER m_gStartTime;
221 static LARGE_INTEGER m_ticksPerSecond;
222 static LARGE_INTEGER m_gElapsedTime;
223 static double m_realFreq;
224
225 LARGE_INTEGER m_startTime;
226 LARGE_INTEGER m_elapsedTime;
227 #else
228 typedef high_resolution_clock::time_point TimePoint;
229 static TimePoint m_gStartTime;
230 static TimePoint m_gElapsedTime;
231
232 TimePoint m_startTime;
233 TimePoint m_elapsedTime;
234 #endif
235
236 };
237 }
238 #endif
OdinAI::Timer::GetGlobalElapsedMillisec
static T GetGlobalElapsedMillisec()
Definition: Timer.h:85
OdinAI::Timer::GetElapsedMillisec
T GetElapsedMillisec()
Definition: Timer.h:163
OdinAI::Timer::GetTime
T GetTime()
Definition: Timer.h:204
OdinAI::Timer::GetGlobalElapsedTime
static T GetGlobalElapsedTime()
Definition: Timer.h:64
OdinAI::Timer
Definition: Timer.h:22
OdinAI::Timer::Reset
void Reset()
Definition: Timer.h:126
OdinAI::Timer::GetElapsedTime
T GetElapsedTime()
Definition: Timer.h:142
OdinAI::Timer::GlobalReset
static void GlobalReset()
Definition: Timer.h:48
OdinAI::Timer::GetGlobalMillisec
static T GetGlobalMillisec()
Definition: Timer.h:107
OdinAI::Timer::GetMillisec
T GetMillisec()
Definition: Timer.h:185
OdinAI::Timer::Init
static bool Init()
Definition: Timer.h:30