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/tal/str/str.h>
5 #include <ccan/tal/grab_file/grab_file.h>
6 #include <ccan/tal/tal.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 timeabs *start,
38 const struct timeabs *stop,
41 return time_to_nsec(time_divide(time_between(*stop, *start), num));
44 int main(int argc, char *argv[])
47 struct timeabs start, stop;
49 char **words, **misswords;
51 words = tal_strsplit(NULL, grab_file(NULL,
52 argv[1] ? argv[1] : "/usr/share/dict/words"), "\n",
55 num = tal_count(words) - 1;
56 /* Note that on my system, num is just > 98304, where we double! */
57 printf("%zu words\n", num);
59 /* Append and prepend last char for miss testing. */
60 misswords = tal_arr(words, char *, num);
61 for (i = 0; i < num; i++) {
64 lastc = words[i][strlen(words[i])-1];
67 misswords[i] = tal_fmt(misswords, "%c%s%c%c",
68 lastc, words[i], lastc, lastc);
71 printf("#01: Initial insert: ");
74 for (i = 0; i < num; i++)
75 htable_str_add(&ht, words[i]);
77 printf(" %zu ns\n", normalize(&start, &stop, num));
79 printf("Bytes allocated: %zu\n",
80 sizeof(ht.raw.table[0]) << ht.raw.bits);
82 printf("#02: Initial lookup (match): ");
85 for (i = 0; i < num; i++)
86 if (htable_str_get(&ht, words[i]) != words[i])
89 printf(" %zu ns\n", normalize(&start, &stop, num));
91 printf("#03: Initial lookup (miss): ");
94 for (i = 0; i < num; i++) {
95 if (htable_str_get(&ht, misswords[i]))
99 printf(" %zu ns\n", normalize(&start, &stop, num));
101 /* Lookups in order are very cache-friendly for judy; try random */
102 printf("#04: Initial lookup (random): ");
105 for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
106 if (htable_str_get(&ht, words[j]) != words[j])
109 printf(" %zu ns\n", normalize(&start, &stop, num));
112 printf("#05: Initial delete all: ");
115 for (i = 0; i < num; i++)
116 if (!htable_str_del(&ht, words[i]))
119 printf(" %zu ns\n", normalize(&start, &stop, num));
121 printf("#06: Initial re-inserting: ");
124 for (i = 0; i < num; i++)
125 htable_str_add(&ht, words[i]);
127 printf(" %zu ns\n", normalize(&start, &stop, num));
130 printf("#07: Deleting first half: ");
133 for (i = 0; i < num; i+=2)
134 if (!htable_str_del(&ht, words[i]))
137 printf(" %zu ns\n", normalize(&start, &stop, num));
139 printf("#08: Adding (a different) half: ");
143 for (i = 0; i < num; i+=2)
144 htable_str_add(&ht, misswords[i]);
146 printf(" %zu ns\n", normalize(&start, &stop, num));
148 printf("#09: Lookup after half-change (match): ");
151 for (i = 1; i < num; i+=2)
152 if (htable_str_get(&ht, words[i]) != words[i])
154 for (i = 0; i < num; i+=2) {
155 if (htable_str_get(&ht, misswords[i]) != misswords[i])
159 printf(" %zu ns\n", normalize(&start, &stop, num));
161 printf("#10: Lookup after half-change (miss): ");
164 for (i = 0; i < num; i+=2)
165 if (htable_str_get(&ht, words[i]))
167 for (i = 1; i < num; i+=2) {
168 if (htable_str_get(&ht, misswords[i]))
172 printf(" %zu ns\n", normalize(&start, &stop, num));
174 /* Hashtables with delete markers can fill with markers over time.
175 * so do some changes to see how it operates in long-term. */
176 printf("#11: Churn 1: ");
178 for (j = 0; j < num; j+=2) {
179 if (!htable_str_del(&ht, misswords[j]))
181 if (!htable_str_add(&ht, words[j]))
185 printf(" %zu ns\n", normalize(&start, &stop, num));
187 printf("#12: Churn 2: ");
189 for (j = 1; j < num; j+=2) {
190 if (!htable_str_del(&ht, words[j]))
192 if (!htable_str_add(&ht, misswords[j]))
196 printf(" %zu ns\n", normalize(&start, &stop, num));
198 printf("#13: Churn 3: ");
200 for (j = 1; j < num; j+=2) {
201 if (!htable_str_del(&ht, misswords[j]))
203 if (!htable_str_add(&ht, words[j]))
207 printf(" %zu ns\n", normalize(&start, &stop, num));
209 /* Now it's back to normal... */
210 printf("#14: Post-Churn lookup (match): ");
213 for (i = 0; i < num; i++)
214 if (htable_str_get(&ht, words[i]) != words[i])
217 printf(" %zu ns\n", normalize(&start, &stop, num));
219 printf("#15: Post-Churn lookup (miss): ");
222 for (i = 0; i < num; i++) {
223 if (htable_str_get(&ht, misswords[i]))
227 printf(" %zu ns\n", normalize(&start, &stop, num));
229 /* Lookups in order are very cache-friendly for judy; try random */
230 printf("#16: Post-Churn lookup (random): ");
233 for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
234 if (htable_str_get(&ht, words[j]) != words[j])
237 printf(" %zu ns\n", normalize(&start, &stop, num));