-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStopwatch.c
70 lines (53 loc) · 1.15 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
61
62
63
64
65
66
67
68
69
#include <malloc.h>
#include "Stopwatch.h"
double seconds() {
return ((double) clock()) / (double) CLOCKS_PER_SEC;
}
void Stopwtach_reset(Stopwatch Q) {
Q->running = 0; /* false */
Q->last_time = 0.0;
Q->total= 0.0;
}
Stopwatch new_Stopwatch(void) {
Stopwatch S = (Stopwatch) malloc(sizeof(Stopwatch_struct));
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 = 1; /* true */
Q->total = 0.0;
Q->last_time = seconds();
}
}
/**
Resume timing, after stopping. (Does not wipe out
accumulated times.)
*/
void Stopwatch_resume(Stopwatch Q) {
if (!(Q->running)) {
Q-> last_time = seconds();
Q->running = 1; /*true*/
}
}
void Stopwatch_stop(Stopwatch Q) {
if (Q->running) {
Q->total += seconds() - Q->last_time;
Q->running = 0; /* 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;
}