-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnixtimer.h
61 lines (45 loc) · 1.17 KB
/
nixtimer.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
#ifndef __NIX_TIMER__
#define __NIX_TIMER__
/*
High-precision timer for Windows or Linux.
Ed Karrels, [email protected]
*/
#ifdef _WIN32
#include <windows.h>
#endif
#include <errno.h>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <ctime>
// Just one method, NixTimer::time()
// Returns a high-precision relative time in seconds
class NixTimer {
#ifndef _WIN32
public:
static double time() {
struct timespec t;
if (clock_gettime(CLOCK_MONOTONIC, &t)) {
fprintf(stderr, "Error calling clock_gettime: %s\n", strerror(errno));
exit(1);
}
return t.tv_sec + 1e-9 * t.tv_nsec;
}
#else // defined(_WIN32)
LARGE_INTEGER frequency;
static NixTimer nixTimerFrequencyObject;
public:
NixTimer() {
if (!QueryPerformanceFrequency(&frequency))
fprintf(stderr,"High-resolution performance counter not supported.",
true);
}
static double time() {
LARGE_INTEGER counter;
if (!QueryPerformanceCounter(&counter))
fprintf(stderr, "error calling QueryPerformanceCounter");
return counter.QuadPart / (double) nixTimerFrequencyObject.frequency.QuadPart;
}
#endif
};
#endif // __NIX_TIMER__