-
Notifications
You must be signed in to change notification settings - Fork 0
/
timers.c
53 lines (37 loc) · 785 Bytes
/
timers.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
49
50
51
52
53
#include <stdio.h>
#include <sys/time.h>
#define NUM_TIMERS 64
static double t_start[NUM_TIMERS];
static double t_elapsed[NUM_TIMERS];
static unsigned t_count[NUM_TIMERS];
static inline double get_time()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (double)tv.tv_sec + (double)1.0e-6*tv.tv_usec;
}
void timer_init() {
int i;
for (i = 0; i < NUM_TIMERS; i++) {
t_start[i] = 0.0;
t_elapsed[i] = 0.0;
t_count[i] = 0;
}
}
void timer_clear(int i) {
t_elapsed[i] = 0.0;
t_count[i] = 0;
}
void timer_start(int i) {
t_start[i] = get_time();
}
void timer_stop(int i) {
t_elapsed[i] += (get_time() - t_start[i]);
t_count[i]++;
}
double timer_read(int i) {
return t_elapsed[i];
}
unsigned timer_count(int i) {
return t_count[i];
}