-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtiming.h
28 lines (25 loc) · 806 Bytes
/
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
#include <time.h>
#include <strings.h>
#define TIME_TO_SEC(x) (((double) x) / 1000000000.0)
#define TIME_TO_MS(x) (((double) x) / 1000000.0)
// call this function to start a nanosecond-resolution timer
static inline struct timespec timer_start(){
struct timespec start_time;
#ifdef COLLECT_STATS
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time);
#else
bzero(&start_time, sizeof(struct timespec));
#endif
return start_time;
}
// call this function to end a timer, returning nanoseconds elapsed as a long
static inline unsigned long timer_end(struct timespec start_time){
#ifdef COLLECT_STATS
struct timespec end_time;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time);
unsigned long diffInNanos = end_time.tv_nsec - start_time.tv_nsec;
return diffInNanos;
#else
return 0;
#endif
}