-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathperf.h
29 lines (23 loc) · 878 Bytes
/
perf.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
#pragma once
#include <stdint.h>
#include <time.h>
#define TIMESPAN_GET_NS(DEST, START, END) \
DEST = -(START.tv_sec * 1e9 + START.tv_nsec) + \
(END.tv_sec * 1e9 + END.tv_nsec);
#define TIMESPAN_MEASURE(NAME) clock_gettime(CLOCK_MONOTONIC, &NAME);
/* Start measuring time (nanosec) to variable "name" */
#define PERF_START(name) \
struct timespec name##_start, name##_end; \
double name; \
TIMESPAN_MEASURE(name##_start);
/* Stop measuring time, store results to variable double "name" */
#define PERF_END(name) \
TIMESPAN_MEASURE(name##_end); \
TIMESPAN_GET_NS(name, name##_start, name##_end)
/* Returns the current time in ms */
static inline uint64_t get_time_ns()
{
struct timespec clk;
TIMESPAN_MEASURE(clk);
return clk.tv_sec * 1e9 + clk.tv_nsec;
}