-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtimer.c
More file actions
58 lines (48 loc) · 1.1 KB
/
timer.c
File metadata and controls
58 lines (48 loc) · 1.1 KB
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
#include "timer.h"
#include "sys.h"
static long long start_time_usec = 0;
static long long accumulated_time_usec = 0;
static bool is_running = false;
void stopwatch_start()
{
if (!is_running)
{
start_time_usec = usectime();
is_running = true;
}
}
void stopwatch_stop()
{
accumulated_time_usec = stopwatch_get_elapsed();
is_running = false;
}
void stopwatch_reset()
{
accumulated_time_usec = 0;
start_time_usec = usectime();
}
long long stopwatch_get_elapsed()
{
return accumulated_time_usec + (is_running? (usectime() - start_time_usec) : 0);
}
bool stopwatch_is_enabled()
{
return accumulated_time_usec || is_running;
}
bool stopwatch_is_running()
{
return is_running;
}
const char *format_time (long long usecs)
{
static char buf[80];
const long long seconds = usecs / 1000000;
const long long minutes = seconds / 60;
const long long hours = minutes / 60;
snprintf(buf, sizeof(buf), "%lli:%02lli:%02lli.%02lli",
hours,
minutes % 60,
seconds % 60,
(usecs/10000) % 100);
return buf;
}