-
Notifications
You must be signed in to change notification settings - Fork 262
/
timer.h
63 lines (47 loc) · 908 Bytes
/
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
#pragma once
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
class Timer {
public:
Timer() : start_(), end_() {
}
void Start() {
QueryPerformanceCounter(&start_);
}
void Stop() {
QueryPerformanceCounter(&end_);
}
double GetElapsedMilliseconds() {
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
return (end_.QuadPart - start_.QuadPart) * 1000.0 / freq.QuadPart;
}
private:
LARGE_INTEGER start_;
LARGE_INTEGER end_;
};
// Undefine Windows bad macros
#undef min
#undef max
#else
#include <sys/time.h>
class Timer {
public:
Timer() : start_(), end_() {
}
void Start() {
gettimeofday(&start_, NULL);
}
void Stop() {
gettimeofday(&end_, NULL);
}
double GetElapsedMilliseconds() {
return (end_.tv_sec - start_.tv_sec) * 1000.0
+ (end_.tv_usec - start_.tv_usec) / 1000.0;
}
private:
struct timeval start_;
struct timeval end_;
};
#endif