-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdin.c
65 lines (51 loc) · 1.58 KB
/
stdin.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
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <time.h>
#define ONE_SEC_NS 1000000000
static uint64_t now() {
struct timespec ts;
// CLOCK_MONOTONIC on OSX only supports microsecond precision, and each nanosecond counts
if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts)) {
fprintf(stderr, "error reading current time\n");
abort();
}
return ts.tv_sec * ONE_SEC_NS + ts.tv_nsec;
}
static uint64_t bench(void (*fn)(void *args), void *args) {
uint64_t start = now();
(*fn)(args);
return now() - start;
}
struct read_stdin_args {
int bufsiz;
};
void read_stdin(void *args) {
struct read_stdin_args *a = args;
volatile char c;
for (int i = 0; i < a->bufsiz; i++) c = getchar();
}
int main(int argc, char const *argv[]) {
(void) argc, (void) argv;
char *env_p2_bufsiz = getenv("BUFSIZ");
long p2_bufsiz = 10; // 1 KiB
if (env_p2_bufsiz) {
char *end = NULL;
p2_bufsiz = strtol(env_p2_bufsiz, &end, 10);
if (env_p2_bufsiz == end) {
fprintf(stderr, "invalid buffer size given: %s\n", env_p2_bufsiz);
exit(EXIT_FAILURE);
}
if (p2_bufsiz < 0 || p2_bufsiz > 30) {
fprintf(stderr, "buffer size out of range: [0, 30]\n");
exit(EXIT_FAILURE);
}
}
int bufsiz = 1 << p2_bufsiz;
if (getenv("DEBUG"))
fprintf(stderr, "using size: %d\n", bufsiz);
setvbuf(stdin, NULL, _IOFBF, bufsiz);
struct read_stdin_args fargs = { bufsiz };
printf("%"PRIu64"\n", bench(read_stdin, &fargs));
return EXIT_SUCCESS;
}