forked from sysprog21/rv32emu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stopwatch.c
60 lines (50 loc) · 1.01 KB
/
Stopwatch.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
54
55
56
57
58
59
60
#include <stdlib.h>
#include "Stopwatch.h"
static double seconds()
{
return ((double) clock()) / (double) CLOCKS_PER_SEC;
}
void Stopwtach_reset(Stopwatch Q)
{
Q->running = false;
Q->last_time = 0.0;
Q->total = 0.0;
}
Stopwatch new_Stopwatch(void)
{
Stopwatch S = (Stopwatch) malloc(sizeof(struct Stopwatch));
if (S == NULL)
return NULL;
Stopwtach_reset(S);
return S;
}
void Stopwatch_delete(Stopwatch S)
{
if (S != NULL)
free(S);
}
/* Start resets the timer to 0.0; use resume for continued total */
void Stopwatch_start(Stopwatch Q)
{
if (!(Q->running)) {
Q->running = true;
Q->total = 0.0;
Q->last_time = seconds();
}
}
void Stopwatch_stop(Stopwatch Q)
{
if (Q->running) {
Q->total += seconds() - Q->last_time;
Q->running = false;
}
}
double Stopwatch_read(Stopwatch Q)
{
if (Q->running) {
double t = seconds();
Q->total += t - Q->last_time;
Q->last_time = t;
}
return Q->total;
}