-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.h
52 lines (42 loc) · 1.22 KB
/
util.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
#ifndef UTIL_H
#define UTIL_H
#include <sys/time.h>
#include <time.h>
inline double tv_to_double(struct timeval *tv) {
return tv->tv_sec + (double) tv->tv_usec / 1000000;
}
inline void double_to_tv(double val, struct timeval *tv) {
long long secs = (long long) val;
long long usecs = (long long) ((val - secs) * 1000000);
tv->tv_sec = secs;
tv->tv_usec = usecs;
}
inline double get_time_accurate() {
#if USE_CLOCK_GETTIME
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
// clock_gettime(CLOCK_REALTIME, &ts);
return ts.tv_sec + (double) ts.tv_nsec / 1000000000;
#else
struct timeval tv;
gettimeofday(&tv, NULL);
return tv_to_double(&tv);
#endif
}
inline double get_time() {
//#if USE_CLOCK_GETTIME
// struct timespec ts;
// clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
// // clock_gettime(CLOCK_REALTIME, &ts);
// return ts.tv_sec + (double) ts.tv_nsec / 1000000000;
//#else
struct timeval tv;
gettimeofday(&tv, NULL);
return tv_to_double(&tv);
//#endif
}
void sleep_time(double duration);
uint64_t fnv_64_buf(const void* buf, size_t len);
inline uint64_t fnv_64(uint64_t in) { return fnv_64_buf(&in, sizeof(in)); }
void generate_key(int n, int length, char *buf);
#endif // UTIL_H