1 /* Simple speed tests for a hash of strings. */
2 #include <ccan/htable/htable_type.h>
3 #include <ccan/htable/htable.c>
4 #include <ccan/str_talloc/str_talloc.h>
5 #include <ccan/grab_file/grab_file.h>
6 #include <ccan/talloc/talloc.h>
7 #include <ccan/hash/hash.h>
8 #include <ccan/time/time.h>
16 static size_t hashcount;
18 static const char *strkey(const char *str)
23 static size_t hash_str(const char *key)
26 return hash(key, strlen(key), 0);
29 static bool cmp(const char *obj, const char *key)
31 return strcmp(obj, key) == 0;
34 HTABLE_DEFINE_TYPE(char, strkey, hash_str, cmp, htable_str);
36 /* Nanoseconds per operation */
37 static size_t normalize(const struct timeval *start,
38 const struct timeval *stop,
43 timersub(stop, start, &diff);
45 /* Floating point is more accurate here. */
46 return (double)(diff.tv_sec * 1000000 + diff.tv_usec)
50 int main(int argc, char *argv[])
53 struct timeval start, stop;
55 char **words, **misswords;
57 words = strsplit(NULL, grab_file(NULL,
58 argv[1] ? argv[1] : "/usr/share/dict/words",
61 num = talloc_array_length(words) - 1;
62 /* Note that on my system, num is just > 98304, where we double! */
63 printf("%zu words\n", num);
65 /* Append and prepend last char for miss testing. */
66 misswords = talloc_array(words, char *, num);
67 for (i = 0; i < num; i++) {
70 lastc = words[i][strlen(words[i])-1];
73 misswords[i] = talloc_asprintf(misswords, "%c%s%c%c",
74 lastc, words[i], lastc, lastc);
77 printf("#01: Initial insert: ");
80 for (i = 0; i < num; i++)
81 htable_str_add(&ht, words[i]);
83 printf(" %zu ns\n", normalize(&start, &stop, num));
85 printf("Bytes allocated: %zu\n",
86 sizeof(ht.raw.table[0]) << ht.raw.bits);
88 printf("#02: Initial lookup (match): ");
91 for (i = 0; i < num; i++)
92 if (htable_str_get(&ht, words[i]) != words[i])
95 printf(" %zu ns\n", normalize(&start, &stop, num));
97 printf("#03: Initial lookup (miss): ");
100 for (i = 0; i < num; i++) {
101 if (htable_str_get(&ht, misswords[i]))
105 printf(" %zu ns\n", normalize(&start, &stop, num));
107 /* Lookups in order are very cache-friendly for judy; try random */
108 printf("#04: Initial lookup (random): ");
111 for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
112 if (htable_str_get(&ht, words[j]) != words[j])
115 printf(" %zu ns\n", normalize(&start, &stop, num));
118 printf("#05: Initial delete all: ");
121 for (i = 0; i < num; i++)
122 if (!htable_str_del(&ht, words[i]))
125 printf(" %zu ns\n", normalize(&start, &stop, num));
127 printf("#06: Initial re-inserting: ");
130 for (i = 0; i < num; i++)
131 htable_str_add(&ht, words[i]);
133 printf(" %zu ns\n", normalize(&start, &stop, num));
136 printf("#07: Deleting first half: ");
139 for (i = 0; i < num; i+=2)
140 if (!htable_str_del(&ht, words[i]))
143 printf(" %zu ns\n", normalize(&start, &stop, num));
145 printf("#08: Adding (a different) half: ");
149 for (i = 0; i < num; i+=2)
150 htable_str_add(&ht, misswords[i]);
152 printf(" %zu ns\n", normalize(&start, &stop, num));
154 printf("#09: Lookup after half-change (match): ");
157 for (i = 1; i < num; i+=2)
158 if (htable_str_get(&ht, words[i]) != words[i])
160 for (i = 0; i < num; i+=2) {
161 if (htable_str_get(&ht, misswords[i]) != misswords[i])
165 printf(" %zu ns\n", normalize(&start, &stop, num));
167 printf("#10: Lookup after half-change (miss): ");
170 for (i = 0; i < num; i+=2)
171 if (htable_str_get(&ht, words[i]))
173 for (i = 1; i < num; i+=2) {
174 if (htable_str_get(&ht, misswords[i]))
178 printf(" %zu ns\n", normalize(&start, &stop, num));
180 /* Hashtables with delete markers can fill with markers over time.
181 * so do some changes to see how it operates in long-term. */
182 printf("#11: Churn 1: ");
184 for (j = 0; j < num; j+=2) {
185 if (!htable_str_del(&ht, misswords[j]))
187 if (!htable_str_add(&ht, words[j]))
191 printf(" %zu ns\n", normalize(&start, &stop, num));
193 printf("#12: Churn 2: ");
195 for (j = 1; j < num; j+=2) {
196 if (!htable_str_del(&ht, words[j]))
198 if (!htable_str_add(&ht, misswords[j]))
202 printf(" %zu ns\n", normalize(&start, &stop, num));
204 printf("#13: Churn 3: ");
206 for (j = 1; j < num; j+=2) {
207 if (!htable_str_del(&ht, misswords[j]))
209 if (!htable_str_add(&ht, words[j]))
213 printf(" %zu ns\n", normalize(&start, &stop, num));
215 /* Now it's back to normal... */
216 printf("#14: Post-Churn lookup (match): ");
219 for (i = 0; i < num; i++)
220 if (htable_str_get(&ht, words[i]) != words[i])
223 printf(" %zu ns\n", normalize(&start, &stop, num));
225 printf("#15: Post-Churn lookup (miss): ");
228 for (i = 0; i < num; i++) {
229 if (htable_str_get(&ht, misswords[i]))
233 printf(" %zu ns\n", normalize(&start, &stop, num));
235 /* Lookups in order are very cache-friendly for judy; try random */
236 printf("#16: Post-Churn lookup (random): ");
239 for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
240 if (htable_str_get(&ht, words[j]) != words[j])
243 printf(" %zu ns\n", normalize(&start, &stop, num));