1 /* Simple speed tests using strset code.
3 * Results on my 32 bit Intel(R) Core(TM) i5 CPU M 560 @ 2.67GHz, gcc 4.5.2:
4 * Run 100 times: Min-Max(Avg)
5 #01: Initial insert: 212-219(214)
6 #02: Initial lookup (match): 161-169(162)
7 #03: Initial lookup (miss): 157-163(158)
8 #04: Initial lookup (random): 450-479(453)
9 #05: Initial delete all: 126-137(128)
10 #06: Initial re-inserting: 193-198(194)
11 #07: Deleting first half: 99-102(99)
12 #08: Adding (a different) half: 143-154(144)
13 #09: Lookup after half-change (match): 183-189(184)
14 #10: Lookup after half-change (miss): 198-212(199)
15 #11: Churn 1: 274-282(276)
16 #12: Churn 2: 279-296(282)
17 #13: Churn 3: 278-294(280)
18 #14: Post-Churn lookup (match): 170-180(171)
19 #15: Post-Churn lookup (miss): 175-186(176)
20 #16: Post-Churn lookup (random): 522-534(525)
22 #include <ccan/tal/str/str.h>
23 #include <ccan/tal/grab_file/grab_file.h>
24 #include <ccan/talloc/talloc.h>
25 #include <ccan/time/time.h>
26 #include <ccan/strset/strset.c>
34 /* Nanoseconds per operation */
35 static size_t normalize(const struct timeabs *start,
36 const struct timeabs *stop,
39 return time_to_nsec(time_divide(time_between(*stop, *start), num));
42 int main(int argc, char *argv[])
45 struct timeabs start, stop;
47 char **words, **misswords;
49 words = tal_strsplit(NULL, grab_file(NULL,
50 argv[1] ? argv[1] : "/usr/share/dict/words"),
53 num = tal_count(words) - 1;
54 printf("%zu words\n", num);
56 /* Append and prepend last char for miss testing. */
57 misswords = tal_arr(words, char *, num);
58 for (i = 0; i < num; i++) {
61 lastc = words[i][strlen(words[i])-1];
64 misswords[i] = tal_fmt(misswords, "%c%s%c%c",
65 lastc, words[i], lastc, lastc);
68 printf("#01: Initial insert: ");
71 for (i = 0; i < num; i++)
72 strset_add(&set, words[i]);
74 printf(" %zu ns\n", normalize(&start, &stop, num));
77 printf("Nodes allocated: %zu (%zu bytes)\n",
78 allocated, allocated * sizeof(critbit0_node));
81 printf("#02: Initial lookup (match): ");
84 for (i = 0; i < num; i++)
85 if (!strset_get(&set, words[i]))
88 printf(" %zu ns\n", normalize(&start, &stop, num));
90 printf("#03: Initial lookup (miss): ");
93 for (i = 0; i < num; i++) {
94 if (strset_get(&set, misswords[i]))
98 printf(" %zu ns\n", normalize(&start, &stop, num));
100 /* Lookups in order are very cache-friendly for judy; try random */
101 printf("#04: Initial lookup (random): ");
104 for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
105 if (!strset_get(&set, words[j]))
108 printf(" %zu ns\n", normalize(&start, &stop, num));
110 printf("#05: Initial delete all: ");
113 for (i = 0; i < num; i++)
114 if (!strset_del(&set, words[i]))
117 printf(" %zu ns\n", normalize(&start, &stop, num));
119 printf("#06: Initial re-inserting: ");
122 for (i = 0; i < num; i++)
123 strset_add(&set, words[i]);
125 printf(" %zu ns\n", normalize(&start, &stop, num));
127 printf("#07: Deleting first half: ");
130 for (i = 0; i < num; i+=2)
131 if (!strset_del(&set, words[i]))
134 printf(" %zu ns\n", normalize(&start, &stop, num));
136 printf("#08: Adding (a different) half: ");
140 for (i = 0; i < num; i+=2)
141 strset_add(&set, misswords[i]);
143 printf(" %zu ns\n", normalize(&start, &stop, num));
145 printf("#09: Lookup after half-change (match): ");
148 for (i = 1; i < num; i+=2)
149 if (!strset_get(&set, words[i]))
151 for (i = 0; i < num; i+=2) {
152 if (!strset_get(&set, misswords[i]))
156 printf(" %zu ns\n", normalize(&start, &stop, num));
158 printf("#10: Lookup after half-change (miss): ");
161 for (i = 0; i < num; i+=2)
162 if (strset_get(&set, words[i]))
164 for (i = 1; i < num; i+=2) {
165 if (strset_get(&set, misswords[i]))
169 printf(" %zu ns\n", normalize(&start, &stop, num));
171 /* Hashtables with delete markers can fill with markers over time.
172 * so do some changes to see how it operates in long-term. */
173 printf("#11: Churn 1: ");
175 for (j = 0; j < num; j+=2) {
176 if (!strset_del(&set, misswords[j]))
178 if (!strset_add(&set, words[j]))
182 printf(" %zu ns\n", normalize(&start, &stop, num));
184 printf("#12: Churn 2: ");
186 for (j = 1; j < num; j+=2) {
187 if (!strset_del(&set, words[j]))
189 if (!strset_add(&set, misswords[j]))
193 printf(" %zu ns\n", normalize(&start, &stop, num));
195 printf("#13: Churn 3: ");
197 for (j = 1; j < num; j+=2) {
198 if (!strset_del(&set, misswords[j]))
200 if (!strset_add(&set, words[j]))
204 printf(" %zu ns\n", normalize(&start, &stop, num));
206 /* Now it's back to normal... */
207 printf("#14: Post-Churn lookup (match): ");
210 for (i = 0; i < num; i++)
211 if (!strset_get(&set, words[i]))
214 printf(" %zu ns\n", normalize(&start, &stop, num));
216 printf("#15: Post-Churn lookup (miss): ");
219 for (i = 0; i < num; i++) {
220 if (strset_get(&set, misswords[i]))
224 printf(" %zu ns\n", normalize(&start, &stop, num));
226 /* Lookups in order are very cache-friendly for judy; try random */
227 printf("#16: Post-Churn lookup (random): ");
230 for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
231 if (!strset_get(&set, words[j]))
234 printf(" %zu ns\n", normalize(&start, &stop, num));