-
Notifications
You must be signed in to change notification settings - Fork 53
/
Timer.h
70 lines (57 loc) · 1.1 KB
/
Timer.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
#ifndef TIMER_H
#define TIMER_H
#ifndef LARGE_INTEGER
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif
class Timer
{
public:
Timer()
{
mStop.QuadPart = mStart.QuadPart = 0;
if (!mFreq.QuadPart) {
QueryPerformanceFrequency(&mFreq);
}
}
bool running()
{
return mStop.QuadPart == 0 && mStart.QuadPart != 0;
}
void start()
{
mStop.QuadPart = 0;
QueryPerformanceCounter(&mStart);
}
void stop()
{
QueryPerformanceCounter(&mStop);
mStop.QuadPart = ((mStop.QuadPart - mStart.QuadPart) * 1000000) / mFreq.QuadPart;
}
const LARGE_INTEGER& nano() const
{
return mStop;
}
long milli() const
{
return static_cast<long>(mStop.QuadPart ? ((mStop.QuadPart + 500) / 1000) : 0);
}
double milli_hp() const
{
return mStop.QuadPart / 1000.0;
}
long sec() const
{
return static_cast<long>(mStop.QuadPart ? ((mStop.QuadPart + 500000) / 1000000) : 0);
}
double sec_hp() const
{
return mStop.QuadPart / 1000000.0;
}
private:
static LARGE_INTEGER mFreq;
LARGE_INTEGER mStart, mStop;
};
__declspec(selectany) LARGE_INTEGER Timer::mFreq = { 0 };
#endif // TIMER_H