stringspeed.c 6.1 KB

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