forked from facebookincubator/ft_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_utime.h
54 lines (40 loc) · 1.07 KB
/
ft_utime.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
/* Copyright (c) Meta Platforms, Inc. and affiliates. */
#ifndef FT_UTIME_H
#define FT_UTIME_H
#include <stddef.h>
typedef uint64_t ustimestamp_t;
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
// NOLINTNEXTLINE
static ustimestamp_t frequency;
// NOLINTNEXTLINE
static void initialize_frequency() {
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
/* Convert frequency to microseconds. */
frequency = (ustimestamp_t)freq.QuadPart / 1000000;
}
// NOLINTNEXTLINE
static ustimestamp_t us_time(void) {
if (frequency == 0) {
initialize_frequency();
}
LARGE_INTEGER counter;
QueryPerformanceCounter(&counter);
return (ustimestamp_t)((ustimestamp_t)counter.QuadPart / frequency);
}
#else
#include <pthread.h>
#include <time.h>
// NOLINTNEXTLINE
static ustimestamp_t us_time(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (ustimestamp_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
}
#endif
// NOLINTNEXTLINE
static int64_t us_difftime(ustimestamp_t end, ustimestamp_t start) {
return end - start;
}
#endif /* FT_UTIME_H */