summaryrefslogtreecommitdiffstats
path: root/test/timer.c
blob: 987f18d00cc5de324c75448997c6b4b6028bc5fd (plain)
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
#include <stdlib.h>
#include "timer.h"

struct _Timer {
    struct timeval  start;
    long            seconds;
    long            useconds;
};


Timer *
timer_new (void)
{
    Timer *t = (Timer *) malloc (sizeof (Timer));
    t->seconds = t->useconds = 0L;
    return t;
}

void
timer_destroy (Timer *t)
{
    free (t);
}

void
timer_start (Timer *t)
{
    gettimeofday(&t->start, NULL);
}

void
timer_stop (Timer *t)
{
    struct timeval end;

    gettimeofday(&end, NULL);
    t->seconds += end.tv_sec - t->start.tv_sec;
    t->useconds += end.tv_usec - t->start.tv_usec;
}

double
timer_get_seconds (Timer *t)
{
    return t->seconds + t->useconds / 1000.0 / 1000.0;
}