-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.cpp
86 lines (62 loc) · 2.06 KB
/
Timer.cpp
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
#include "Timer.h"
#include <iostream>
#ifdef max
#undef max
#endif
#if defined WIN32
//------------------------------------------------------------------------
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::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);
}
//------------------------------------------------------------------------
long long int Timer::getElapsedTicks(void)
{
long long int curr = queryTicks();
if (m_startTicks == -1)
m_startTicks = curr;
return curr - m_startTicks;
}
//------------------------------------------------------------------------
#else
float Timer::getElapsed()
{
double elapsedTime = 0.0;
timeval t2 = {0,0};
gettimeofday(&t2, nullptr);
timeval t1 = m_timeVal;
// compute and print the elapsed time in millisec
elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0; // sec to ms
elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0; // us to ms
return elapsedTime*0.001f;
}
#endif