-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtimer.h
56 lines (43 loc) · 1.21 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
#ifndef timer_H__
#define timer_H__
#include <stdint.h>
#if __linux__ != 0
#include <time.h>
static uint64_t timer_ns() {
#if defined(CLOCK_MONOTONIC_RAW)
const clockid_t clockid = CLOCK_MONOTONIC_RAW;
#else
const clockid_t clockid = CLOCK_MONOTONIC;
#endif
timespec t;
clock_gettime(clockid, &t);
return 1000000000ULL * t.tv_sec + t.tv_nsec;
}
#elif _WIN64 != 0
#define NOMINMAX
#include <Windows.h>
static struct TimerBase {
LARGE_INTEGER freq;
TimerBase() { QueryPerformanceFrequency(&freq); }
} timerBase;
// the order of global initialisaitons is non-deterministic, do
// not use this routine in the ctors of globally-scoped objects
static uint64_t timer_ns() {
LARGE_INTEGER t;
QueryPerformanceCounter(&t);
return 1000000000ULL * t.QuadPart / timerBase.freq.QuadPart;
}
#elif __APPLE__ != 0
#include <mach/mach_time.h>
static struct TimerBase {
mach_timebase_info_data_t tb;
TimerBase() { mach_timebase_info(&tb); }
} timerBase;
// the order of global initialisaitons is non-deterministic, do
// not use this routine in the ctors of globally-scoped objects
static uint64_t timer_ns() {
const uint64_t t = mach_absolute_time();
return t * timerBase.tb.numer / timerBase.tb.denom;
}
#endif
#endif // timer_H__