-
Notifications
You must be signed in to change notification settings - Fork 13
/
mapi.c
72 lines (59 loc) · 1.31 KB
/
mapi.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
70
71
72
#include <stdio.h>
#include <time.h>
#include <assert.h>
#include <dynamic.h>
static uint64_t nano_time(void)
{
struct timespec ts;
(void) clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
return ((uint64_t) ts.tv_sec * 1000000000) + ((uint64_t) ts.tv_nsec);
}
static void release(mapi_entry *e)
{
(void) e;
}
int main(int argc, char **argv)
{
uintptr_t *keys, *values, p;
mapi m;
uint64_t n, i;
uint64_t t1, t2;
if (argc != 2)
exit(1);
n = strtoul(argv[1], NULL, 0);
// create keys/values
keys = calloc(n, sizeof keys[0]);
values = calloc(n, sizeof values[0]);
for (i = 0; i < n; i++)
{
keys[i] = i + 1;
values[i] = i + 1;
}
// construct map
mapi_construct(&m);
// insert key->value mappings
t1 = nano_time();
for (i = 0; i < n; i++)
mapi_insert(&m, keys[i], values[i], release);
t2 = nano_time();
printf("insert %lu\n", t2 - t1);
// lookup key and validate value
t1 = nano_time();
for (i = 0; i < n; i++)
{
p = mapi_at(&m, keys[i]);
assert(p == values[i]);
}
t2 = nano_time();
printf("lookup %lu\n", t2 - t1);
t1 = nano_time();
for (i = 0; i < n; i++)
mapi_erase(&m, keys[i], release);
t2 = nano_time();
printf("erase %lu\n", t2 - t1);
// release keys/values
free(keys);
free(values);
// destruct map
mapi_destruct(&m, release);
}