-
Notifications
You must be signed in to change notification settings - Fork 8
/
bm.c
46 lines (41 loc) · 1.13 KB
/
bm.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
// Simple benchmark library.
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
static struct timespec bm_tp[2];
void bm_init() {
clock_gettime(CLOCK_MONOTONIC, bm_tp);
}
void bm_report(char *msg) {
clock_gettime(CLOCK_MONOTONIC, bm_tp + 1);
printf("%s: %ld.%09lds\n", msg,
bm_tp[1].tv_sec - bm_tp[0].tv_sec - (bm_tp[1].tv_nsec < bm_tp[0].tv_nsec),
bm_tp[1].tv_nsec - bm_tp[0].tv_nsec
+ (bm_tp[1].tv_nsec < bm_tp[0].tv_nsec) * 1000000000L);
clock_gettime(CLOCK_MONOTONIC, bm_tp);
}
void bm_read_keys(void (*cb)(char **key, int m)) {
char **key;
int max = 8, m = 0;
key = malloc(sizeof(*key) * max);
for (;;) {
char *s = 0;
size_t n;
ssize_t len = getline(&s, &n, stdin);
if (feof(stdin)) break;
if (len == -1) perror("getline"), exit(1);
if (s[len - 1] == '\n') s[len - 1] = 0;
key[m++] = s;
if (m == max) max *= 2, key = realloc(key, sizeof(*key) * max);
}
// Randomize order of array.
for (int i = m-1; i>1; i--) {
int j = random() % i;
char *tmp = key[i];
key[i] = key[j];
key[j] = tmp;
}
cb(key, m);
}