-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbenchmark_map.c
68 lines (55 loc) · 1.12 KB
/
benchmark_map.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
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "btree.h"
#include "hash.h"
#include "red_black.h"
map_t *init_map(const char *choice)
{
if(strncmp(choice, "btree", 5) == 0) {
int n = atoi(&choice[5]);
return init_btree(n);
}
else if (strcmp(choice, "hash") == 0) {
return init_hash();
}
else if (strcmp(choice, "red_black") == 0) {
return init_red_black();
}
else {
fprintf(stderr, "Invalid choice %s\n", choice);
return NULL;
}
}
void benchmark(map_t *map, int n)
// Insert and lookup n items
{
int *items = malloc(sizeof(int)*n);
for(int i = 0; i < n; i++) {
items[i] = rand();
map_set(map, items[i], &items[i]);
}
bool found;
for(int i = 0; i < n; i++) {
int *item_ptr = map_get(map, items[i], &found);
assert(found);
assert(item_ptr == &items[i]);
}
free(items);
}
int main(int argc, char *argv[])
{
assert(argc == 3);
char *map_choice = argv[1];
int n_entries = atoi(argv[2]);
assert(n_entries > 0);
map_t *m = init_map(map_choice);
if(!m) {
return -1;
}
benchmark(m, n_entries);
map_free(m);
return 0;
}