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 printf("%zu words\n", num);
64 /* Append and prepend last char for miss testing. */
65 misswords = talloc_array(words, char *, num);
66 for (i = 0; i < num; i++) {
69 lastc = words[i][strlen(words[i])-1];
72 misswords[i] = talloc_asprintf(misswords, "%c%s%c%c",
73 lastc, words[i], lastc, lastc);
76 printf("#01: Initial insert: ");
79 for (i = 0; i < num; i++)
80 htable_str_add(&ht, words[i]);
82 printf(" %zu ns\n", normalize(&start, &stop, num));
84 printf("Bytes allocated: %zu\n",
85 sizeof(ht.raw.table[0]) << ht.raw.bits);
87 printf("#02: Initial lookup (match): ");
90 for (i = 0; i < num; i++)
91 if (htable_str_get(&ht, words[i]) != words[i])
94 printf(" %zu ns\n", normalize(&start, &stop, num));
96 printf("#03: Initial lookup (miss): ");
99 for (i = 0; i < num; i++) {
100 if (htable_str_get(&ht, misswords[i]))
104 printf(" %zu ns\n", normalize(&start, &stop, num));
106 /* Lookups in order are very cache-friendly for judy; try random */
107 printf("#04: Initial lookup (random): ");
110 for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
111 if (htable_str_get(&ht, words[j]) != words[j])
114 printf(" %zu ns\n", normalize(&start, &stop, num));
117 printf("#05: Initial delete all: ");
120 for (i = 0; i < num; i++)
121 if (!htable_str_del(&ht, words[i]))
124 printf(" %zu ns\n", normalize(&start, &stop, num));
126 printf("#06: Initial re-inserting: ");
129 for (i = 0; i < num; i++)
130 htable_str_add(&ht, words[i]);
132 printf(" %zu ns\n", normalize(&start, &stop, num));
135 printf("#07: Deleting first half: ");
138 for (i = 0; i < num; i+=2)
139 if (!htable_str_del(&ht, words[i]))
142 printf(" %zu ns\n", normalize(&start, &stop, num));
144 printf("#08: Adding (a different) half: ");
148 for (i = 0; i < num; i+=2)
149 htable_str_add(&ht, misswords[i]);
151 printf(" %zu ns\n", normalize(&start, &stop, num));
153 printf("#09: Lookup after half-change (match): ");
156 for (i = 1; i < num; i+=2)
157 if (htable_str_get(&ht, words[i]) != words[i])
159 for (i = 0; i < num; i+=2) {
160 if (htable_str_get(&ht, misswords[i]) != misswords[i])
164 printf(" %zu ns\n", normalize(&start, &stop, num));
166 printf("#10: Lookup after half-change (miss): ");
169 for (i = 0; i < num; i+=2)
170 if (htable_str_get(&ht, words[i]))
172 for (i = 1; i < num; i+=2) {
173 if (htable_str_get(&ht, misswords[i]))
177 printf(" %zu ns\n", normalize(&start, &stop, num));
179 /* Hashtables with delete markers can fill with markers over time.
180 * so do some changes to see how it operates in long-term. */
181 printf("#11: Churn 1: ");
183 for (j = 0; j < num; j+=2) {
184 if (!htable_str_del(&ht, misswords[j]))
186 if (!htable_str_add(&ht, words[j]))
190 printf(" %zu ns\n", normalize(&start, &stop, num));
192 printf("#12: Churn 2: ");
194 for (j = 1; j < num; j+=2) {
195 if (!htable_str_del(&ht, words[j]))
197 if (!htable_str_add(&ht, misswords[j]))
201 printf(" %zu ns\n", normalize(&start, &stop, num));
203 printf("#13: Churn 3: ");
205 for (j = 1; j < num; j+=2) {
206 if (!htable_str_del(&ht, misswords[j]))
208 if (!htable_str_add(&ht, words[j]))
212 printf(" %zu ns\n", normalize(&start, &stop, num));
214 /* Now it's back to normal... */
215 printf("#14: Post-Churn lookup (match): ");
218 for (i = 0; i < num; i++)
219 if (htable_str_get(&ht, words[i]) != words[i])
222 printf(" %zu ns\n", normalize(&start, &stop, num));
224 printf("#15: Post-Churn lookup (miss): ");
227 for (i = 0; i < num; i++) {
228 if (htable_str_get(&ht, misswords[i]))
232 printf(" %zu ns\n", normalize(&start, &stop, num));
234 /* Lookups in order are very cache-friendly for judy; try random */
235 printf("#16: Post-Churn lookup (random): ");
238 for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
239 if (htable_str_get(&ht, words[j]) != words[j])
242 printf(" %zu ns\n", normalize(&start, &stop, num));