-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimer.h
50 lines (40 loc) · 850 Bytes
/
timer.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
#ifndef TIMER_H
#define TIMER_H
/*
* timer wrappers
* kkourt@cslab.ece.ntua.gr
*/
#include <sys/time.h>
struct timer {
struct timeval tv_start;
unsigned long usecs_total;
unsigned long usecs_last;
unsigned long pauses;
};
typedef struct timer xtimer_t;
static inline void timer_init(xtimer_t *t)
{
t->usecs_total = 0;
t->usecs_last = 0;
t->pauses = 0;
}
static inline void timer_start(xtimer_t *t)
{
gettimeofday(&t->tv_start, NULL);
}
static inline void timer_pause(xtimer_t *t)
{
struct timeval tv_stop;
gettimeofday(&tv_stop, NULL);
unsigned long usecs = (tv_stop.tv_sec - t->tv_start.tv_sec)*1000000;
usecs += tv_stop.tv_usec;
usecs -= t->tv_start.tv_usec;
t->usecs_last = usecs;
t->usecs_total += usecs;
t->pauses++;
}
static double timer_secs(xtimer_t *t)
{
return (double)t->usecs_total/(double)1000000;
}
#endif