-
Notifications
You must be signed in to change notification settings - Fork 0
/
timing.h
54 lines (42 loc) · 1.23 KB
/
timing.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
#ifdef __APPLE__
#include <sys/time.h>
typedef struct timeval timestamp_type;
static void get_timestamp(timestamp_type *t)
{
gettimeofday(t, NULL);
}
double timestamp_diff_in_seconds(timestamp_type start, timestamp_type end)
{
/* Perform the carry for the later subtraction by updating start. */
if (end.tv_usec < start.tv_usec) {
int nsec = (start.tv_usec - end.tv_usec) / 1000000 + 1;
start.tv_usec -= 1000000 * nsec;
start.tv_sec += nsec;
}
if (end.tv_usec - start.tv_usec > 1000000) {
int nsec = (end.tv_usec - start.tv_usec) / 1000000;
start.tv_usec += 1000000 * nsec;
start.tv_sec -= nsec;
}
return end.tv_sec - start.tv_sec + (end.tv_usec - start.tv_usec)*1e-6;
}
#else
#include <time.h>
typedef struct timespec timestamp_type;
static void get_timestamp(timestamp_type *t)
{
clock_gettime(CLOCK_REALTIME, t);
}
static double timestamp_diff_in_seconds(timestamp_type start, timestamp_type end)
{
struct timespec temp;
if ((end.tv_nsec-start.tv_nsec)<0) {
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return temp.tv_sec + 1e-9*temp.tv_nsec;
}
#endif