-
Notifications
You must be signed in to change notification settings - Fork 2
/
nanosec.c
48 lines (35 loc) · 866 Bytes
/
nanosec.c
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
#include <stdint.h>
#include <time.h>
#include "nanosec.h"
uint64_t time_in_secs(uint64_t time) {
return time / 1e9;
}
#ifdef MAC
#include <mach/mach_time.h>
uint64_t time_get()
{
return mach_absolute_time();
}
float time_diff(uint64_t end,
uint64_t start) {
uint64_t difference = end - start;
static mach_timebase_info_data_t info = {0,0};
if (info.denom == 0)
mach_timebase_info(&info);
uint64_t elapsednano = difference * (info.numer / info.denom);
return elapsednano * 1e-9;
}
#else
uint64_t time_get()
{
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return t.tv_sec * 1e9 + t.tv_nsec;
}
float time_diff(uint64_t end,
uint64_t start)
{
float elapsednano = end - start;
return elapsednano * 1e-9;
}
#endif