speed.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* Simple speed tests using strset code.
  2. *
  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)
  21. */
  22. #include <ccan/str_talloc/str_talloc.h>
  23. #include <ccan/grab_file/grab_file.h>
  24. #include <ccan/talloc/talloc.h>
  25. #include <ccan/time/time.h>
  26. #include <ccan/strset/strset.c>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <time.h>
  31. #include <unistd.h>
  32. #include <sys/time.h>
  33. /* Nanoseconds per operation */
  34. static size_t normalize(const struct timeval *start,
  35. const struct timeval *stop,
  36. unsigned int num)
  37. {
  38. struct timeval diff;
  39. timersub(stop, start, &diff);
  40. /* Floating point is more accurate here. */
  41. return (double)(diff.tv_sec * 1000000 + diff.tv_usec)
  42. / num * 1000;
  43. }
  44. int main(int argc, char *argv[])
  45. {
  46. size_t i, j, num;
  47. struct timeval start, stop;
  48. struct strset set;
  49. char **words, **misswords;
  50. words = strsplit(NULL, grab_file(NULL,
  51. argv[1] ? argv[1] : "/usr/share/dict/words",
  52. NULL), "\n");
  53. strset_init(&set);
  54. num = talloc_array_length(words) - 1;
  55. printf("%zu words\n", num);
  56. /* Append and prepend last char for miss testing. */
  57. misswords = talloc_array(words, char *, num);
  58. for (i = 0; i < num; i++) {
  59. char lastc;
  60. if (strlen(words[i]))
  61. lastc = words[i][strlen(words[i])-1];
  62. else
  63. lastc = 'z';
  64. misswords[i] = talloc_asprintf(misswords, "%c%s%c%c",
  65. lastc, words[i], lastc, lastc);
  66. }
  67. printf("#01: Initial insert: ");
  68. fflush(stdout);
  69. start = time_now();
  70. for (i = 0; i < num; i++)
  71. strset_set(&set, words[i]);
  72. stop = time_now();
  73. printf(" %zu ns\n", normalize(&start, &stop, num));
  74. #if 0
  75. printf("Nodes allocated: %zu (%zu bytes)\n",
  76. allocated, allocated * sizeof(critbit0_node));
  77. #endif
  78. printf("#02: Initial lookup (match): ");
  79. fflush(stdout);
  80. start = time_now();
  81. for (i = 0; i < num; i++)
  82. if (!strset_test(&set, words[i]))
  83. abort();
  84. stop = time_now();
  85. printf(" %zu ns\n", normalize(&start, &stop, num));
  86. printf("#03: Initial lookup (miss): ");
  87. fflush(stdout);
  88. start = time_now();
  89. for (i = 0; i < num; i++) {
  90. if (strset_test(&set, misswords[i]))
  91. abort();
  92. }
  93. stop = time_now();
  94. printf(" %zu ns\n", normalize(&start, &stop, num));
  95. /* Lookups in order are very cache-friendly for judy; try random */
  96. printf("#04: Initial lookup (random): ");
  97. fflush(stdout);
  98. start = time_now();
  99. for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
  100. if (!strset_test(&set, words[j]))
  101. abort();
  102. stop = time_now();
  103. printf(" %zu ns\n", normalize(&start, &stop, num));
  104. printf("#05: Initial delete all: ");
  105. fflush(stdout);
  106. start = time_now();
  107. for (i = 0; i < num; i++)
  108. if (!strset_clear(&set, words[i]))
  109. abort();
  110. stop = time_now();
  111. printf(" %zu ns\n", normalize(&start, &stop, num));
  112. printf("#06: Initial re-inserting: ");
  113. fflush(stdout);
  114. start = time_now();
  115. for (i = 0; i < num; i++)
  116. strset_set(&set, words[i]);
  117. stop = time_now();
  118. printf(" %zu ns\n", normalize(&start, &stop, num));
  119. printf("#07: Deleting first half: ");
  120. fflush(stdout);
  121. start = time_now();
  122. for (i = 0; i < num; i+=2)
  123. if (!strset_clear(&set, words[i]))
  124. abort();
  125. stop = time_now();
  126. printf(" %zu ns\n", normalize(&start, &stop, num));
  127. printf("#08: Adding (a different) half: ");
  128. fflush(stdout);
  129. start = time_now();
  130. for (i = 0; i < num; i+=2)
  131. strset_set(&set, misswords[i]);
  132. stop = time_now();
  133. printf(" %zu ns\n", normalize(&start, &stop, num));
  134. printf("#09: Lookup after half-change (match): ");
  135. fflush(stdout);
  136. start = time_now();
  137. for (i = 1; i < num; i+=2)
  138. if (!strset_test(&set, words[i]))
  139. abort();
  140. for (i = 0; i < num; i+=2) {
  141. if (!strset_test(&set, misswords[i]))
  142. abort();
  143. }
  144. stop = time_now();
  145. printf(" %zu ns\n", normalize(&start, &stop, num));
  146. printf("#10: Lookup after half-change (miss): ");
  147. fflush(stdout);
  148. start = time_now();
  149. for (i = 0; i < num; i+=2)
  150. if (strset_test(&set, words[i]))
  151. abort();
  152. for (i = 1; i < num; i+=2) {
  153. if (strset_test(&set, misswords[i]))
  154. abort();
  155. }
  156. stop = time_now();
  157. printf(" %zu ns\n", normalize(&start, &stop, num));
  158. /* Hashtables with delete markers can fill with markers over time.
  159. * so do some changes to see how it operates in long-term. */
  160. printf("#11: Churn 1: ");
  161. start = time_now();
  162. for (j = 0; j < num; j+=2) {
  163. if (!strset_clear(&set, misswords[j]))
  164. abort();
  165. if (!strset_set(&set, words[j]))
  166. abort();
  167. }
  168. stop = time_now();
  169. printf(" %zu ns\n", normalize(&start, &stop, num));
  170. printf("#12: Churn 2: ");
  171. start = time_now();
  172. for (j = 1; j < num; j+=2) {
  173. if (!strset_clear(&set, words[j]))
  174. abort();
  175. if (!strset_set(&set, misswords[j]))
  176. abort();
  177. }
  178. stop = time_now();
  179. printf(" %zu ns\n", normalize(&start, &stop, num));
  180. printf("#13: Churn 3: ");
  181. start = time_now();
  182. for (j = 1; j < num; j+=2) {
  183. if (!strset_clear(&set, misswords[j]))
  184. abort();
  185. if (!strset_set(&set, words[j]))
  186. abort();
  187. }
  188. stop = time_now();
  189. printf(" %zu ns\n", normalize(&start, &stop, num));
  190. /* Now it's back to normal... */
  191. printf("#14: Post-Churn lookup (match): ");
  192. fflush(stdout);
  193. start = time_now();
  194. for (i = 0; i < num; i++)
  195. if (!strset_test(&set, words[i]))
  196. abort();
  197. stop = time_now();
  198. printf(" %zu ns\n", normalize(&start, &stop, num));
  199. printf("#15: Post-Churn lookup (miss): ");
  200. fflush(stdout);
  201. start = time_now();
  202. for (i = 0; i < num; i++) {
  203. if (strset_test(&set, misswords[i]))
  204. abort();
  205. }
  206. stop = time_now();
  207. printf(" %zu ns\n", normalize(&start, &stop, num));
  208. /* Lookups in order are very cache-friendly for judy; try random */
  209. printf("#16: Post-Churn lookup (random): ");
  210. fflush(stdout);
  211. start = time_now();
  212. for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
  213. if (!strset_test(&set, words[j]))
  214. abort();
  215. stop = time_now();
  216. printf(" %zu ns\n", normalize(&start, &stop, num));
  217. return 0;
  218. }