-
Notifications
You must be signed in to change notification settings - Fork 5
/
bench.h
87 lines (75 loc) · 1.41 KB
/
bench.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* @file bench.h
*
* @brief benchmarking utils
*
* @detail
* usage:
* bench_t b;
* bench_init(b); // clear accumulator
*
* bench_start(b);
* // execution time between bench_start and bench_end is accumulated
* bench_end(b);
*
* printf("%lld us\n", bench_get(b)); // in us
*/
#ifndef _BENCH_H_INCLUDED
#define _BENCH_H_INCLUDED
#include <stdint.h>
#include <string.h>
/**
* benchmark macros
*/
#ifdef BENCH
#include <sys/time.h>
/**
* @struct _bench
* @brief benchmark variable container
*/
struct _bench {
struct timeval s; /** start */
int64_t a; /** accumulator */
};
typedef struct _bench bench_t;
/**
* @macro bench_init
*/
#define bench_init(b) { \
memset(&(b).s, 0, sizeof(struct timeval)); \
(b).a = 0; \
}
/**
* @macro bench_start
*/
#define bench_start(b) { \
gettimeofday(&(b).s, NULL); \
}
/**
* @macro bench_end
*/
#define bench_end(b) { \
struct timeval _e; \
gettimeofday(&_e, NULL); \
(b).a += ( (_e.tv_sec - (b).s.tv_sec ) * 1000000000 \
+ (_e.tv_usec - (b).s.tv_usec) * 1000); \
}
/**
* @macro bench_get
*/
#define bench_get(b) ( \
(b).a \
)
#else /* #ifdef BENCH */
/** disable bench */
struct _bench { uint64_t _unused; };
typedef struct _bench bench_t;
#define bench_init(b) ;
#define bench_start(b) ;
#define bench_end(b) ;
#define bench_get(b) ( 0LL )
#endif /* #ifdef BENCH */
#endif /* #ifndef _BENCH_H_INCLUDED */
/**
* end of bench.h
*/