-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTimer.cpp
More file actions
98 lines (69 loc) · 2.04 KB
/
Timer.cpp
File metadata and controls
98 lines (69 loc) · 2.04 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
#include "Timer.h"
#include <stdexcept>
#ifdef WIN32
#include <windows.h>
#ifdef max
#undef max
#endif
#ifdef min
#undef min
#endif
#endif
//------------------------------------------------------------------------
double Timer::s_ticksToSecsCoef = -1.0;
long long int Timer::s_prevTicks = 0;
//------------------------------------------------------------------------
float Timer::end(void)
{
long long int elapsed = getElapsedTicks();
m_startTicks += elapsed;
m_totalTicks += elapsed;
return ticksToSecs(elapsed);
}
inline long long int max(long long int a, long long int b) { return a > b ? a : b; }
inline double max(double a, double b) { return a > b ? a : b; }
long long int Timer::getElapsedTicks(void)
{
long long int curr = queryTicks();
if (m_startTicks == -1)
m_startTicks = curr;
return curr - m_startTicks;
}
#ifdef WIN32
//------------------------------------------------------------------------
long long int Timer::queryTicks(void)
{
LARGE_INTEGER ticks;
if (!QueryPerformanceCounter(&ticks))
throw std::runtime_error("QueryPerformanceFrequency failed");
s_prevTicks = max(s_prevTicks, ticks.QuadPart);
return s_prevTicks;
}
//------------------------------------------------------------------------
float Timer::ticksToSecs(long long int ticks)
{
if (s_ticksToSecsCoef == -1.0)
{
LARGE_INTEGER freq;
if (!QueryPerformanceFrequency(&freq))
throw std::runtime_error("QueryPerformanceFrequency failed");
s_ticksToSecsCoef = max(1.0 / (double)freq.QuadPart, 0.0);
}
return (float)(ticks * s_ticksToSecsCoef);
}
//------------------------------------------------------------------------
#else
#include <time.h>
long long int Timer::queryTicks(void)
{
s_prevTicks = clock();
return 0;
}
//------------------------------------------------------------------------
float Timer::ticksToSecs(long long int ticks)
{
const clock_t end_time = clock();
float elapsed = float(end_time - s_prevTicks)/CLOCKS_PER_SEC;
return elapsed;
}
#endif