forked from OwseiWasTaken/devaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.c
68 lines (57 loc) · 1.55 KB
/
timer.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
typedef struct {
char* label;
float total;
} timer;
// SoA could be faster than AoS
//typedef struct {
// char* label[];
// float total[];
//} timers;
int main(int argc, char *argv[]) {
char cursor_reset[50];
timer timers[argc];
struct timespec span = {0};
long double tspan = 0;
struct timespec start = {0};
struct timespec now = {0};
// 0.1s
struct timespec step = { .tv_nsec = 100000000 };
clock_gettime(CLOCK_MONOTONIC, &start);
sprintf(cursor_reset, "\x1b[%dA", argc);
for (int i = 1; i<argc; i++) {
char *time = strtok(argv[i], " :-)");
char *label = strtok(NULL, "");
timer a = { .label = label?label:"timer", .total = atof(time) };
timers[i] = a;
}
int still_awaiting = 1;
while (still_awaiting) {
clock_gettime(CLOCK_MONOTONIC, &now);
span.tv_sec = now.tv_sec - start.tv_sec;
span.tv_nsec = (now.tv_nsec - start.tv_nsec)/100000;
tspan = (long double)span.tv_sec + ((long double)span.tv_nsec)/10000;
//printf("%Lf:%Lf\n", (long double)span.tv_sec, ((long double)span.tv_nsec)/1000);
still_awaiting = 0;
for (int i = 1; i<argc; i++) {
if (timers[i].total - tspan <= 0) {
timers[i].total = 0;
}
if (timers[i].total == 0) {
printf("%d: %s: completed!\n", i, timers[i].label);
} else {
still_awaiting = 1;
printf("%d: %s: %.4Lf\n", i, timers[i].label, timers[i].total-tspan);
}
}
puts(cursor_reset);
clock_nanosleep(CLOCK_MONOTONIC, 0, &step, NULL);
}
for (int i = 1; i<argc; i++) {
putc('\n', stdout);
}
return 0;
}