Advertisement
avp210159

t2hash.c compare 2 hashing methods

Nov 12th, 2015
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.36 KB | None | 0 0
  1. // avp/src/dispro/hash_misc/t2hash.c
  2. // compare hashing step 1  with step hash * 2 + 1
  3. // add av[1] (1000000) rand to htab[get_pow2size(av[1])],
  4. // then search 2 * av[1] rand (from initial rand)
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <time.h>
  10. #include <limits.h>
  11.  
  12. int
  13. add1 (int ht[], int size, int key)
  14. {
  15.   int i = key % size;
  16.  
  17.   for (; ht[i] != -1; i = ++i % size)
  18.     if (ht[i] == key)
  19.       return 0;
  20.   ht[i] = key;
  21.   return 1;
  22. }
  23.  
  24. int
  25. add2 (int ht[], int size, int key)
  26. {
  27.   int i = key % size, step = (2 * i + 1) % size;
  28.  
  29.   for (; ht[i] != -1; i = (i + step) % size)
  30.     if (ht[i] == key)
  31.       return 0;
  32.   ht[i] = key;
  33.   return 1;
  34. }
  35.  
  36. int
  37. look1 (int ht[], int size, int key)
  38. {
  39.   int i = key % size;
  40.  
  41.   for (; ht[i] != -1; i = ++i % size)
  42.     if (ht[i] == key)
  43.       return 1;
  44.  return 0;
  45. }
  46.  
  47. int
  48. look2 (int ht[], int size, int key)
  49. {
  50.   int i = key % size, step = (2 * i + 1) % size;
  51.  
  52.   for (; ht[i] != -1; i = (i + step) % size)
  53.     if (ht[i] == key)
  54.       return 1;
  55.   return 0;
  56. }
  57.  
  58. void
  59. measure (int size, int *ht, int n, int (*add)(), int (*look)(), char *msg)
  60. {
  61.   int i, nn, k;
  62.  
  63.   puts(msg);
  64.   clock_t start = clock();
  65.   srand(0);
  66.   for (k = i = 0; i < n; i++)
  67.     k += add(ht, size, rand());
  68.   printf("add %d (%f)  time %f sec\n",
  69.      k, (double)k / size, (double)(clock() - start) / CLOCKS_PER_SEC);
  70.   srand(0);
  71.   for (nn = i = 0; i < 2 * n; i++)
  72.     nn += look(ht, size, rand());
  73.  
  74.  
  75.  printf("total time %f sec   search %d loops found %d (%f)\n",
  76.     (double)(clock() - start) / CLOCKS_PER_SEC,
  77.     2 * n, nn, (double)nn / (2 * n));
  78. }
  79.  
  80. int
  81. getsize (int n)
  82. {
  83.   int i = 1;
  84.  
  85.   while (i > 0 && i <= n)
  86.     i <<= 1;
  87.  
  88.   return i > 0 ? i : INT_MAX;
  89. }
  90.  
  91. int
  92. main (int ac, char *av[])
  93. {
  94.   int n = atoi(av[1] ? av[1] : "1000000");
  95.   if (n < 1)
  96.     n = 1000000;
  97.  
  98.   int hsize = getsize(n),
  99.     *ht = malloc(hsize * sizeof(int));
  100.  
  101.   printf("n = %d htsize = %d (%f)\n", n, hsize, (double)n / hsize);
  102.   memset(ht, -1, hsize * sizeof(int));
  103.   measure(hsize, ht, n, add1, look1, "step 1 hashing");
  104.   memset(ht, -1, hsize * sizeof(int));
  105.   measure(hsize, ht, n, add2, look2, "hash * 2 + 1 step hashing");
  106.  
  107.  return puts("End") == EOF;
  108. }
  109.  
  110.  
  111. /*
  112. avp@avp-ubu1:hash_misc$ ./a.out 400000000
  113. n = 400000000 htsize = 536870912 (0.745058)
  114. step 1 hashing
  115. add 364953976 (0.679780)  time 29.271316 sec
  116. total time 102.030381 sec   search 800000000 loops found 467981948 (0.584977)
  117. hash * 2 + 1 step hashing
  118. add 364953976 (0.679780)  time 37.493039 sec
  119. total time 129.154172 sec   search 800000000 loops found 467981948 (0.584977)
  120. End
  121. avp@avp-ubu1:hash_misc$ ./a.out 4000000
  122. n = 4000000 htsize = 4194304 (0.953674)
  123. step 1 hashing
  124. add 3996283 (0.952788)  time 0.518494 sec
  125. total time 7.049557 sec   search 8000000 loops found 4007329 (0.500916)
  126. hash * 2 + 1 step hashing
  127. add 3996283 (0.952788)  time 0.413647 sec
  128. total time 2.192685 sec   search 8000000 loops found 4007329 (0.500916)
  129. End
  130. avp@avp-ubu1:hash_misc$ ./a.out 2500000
  131. n = 2500000 htsize = 4194304 (0.596046)
  132. step 1 hashing
  133. add 2498560 (0.595703)  time 0.151391 sec
  134. total time 0.512339 sec   search 5000000 loops found 2502925 (0.500585)
  135. hash * 2 + 1 step hashing
  136. add 2498560 (0.595703)  time 0.179143 sec
  137. total time 0.611102 sec   search 5000000 loops found 2502925 (0.500585)
  138. End
  139. avp@avp-ubu1:hash_misc$ ./a.out
  140. n = 1000000 htsize = 1048576 (0.953674)
  141. step 1 hashing
  142. add 999752 (0.953438)  time 0.123139 sec
  143. total time 1.688386 sec   search 2000000 loops found 1000469 (0.500235)
  144. hash * 2 + 1 step hashing
  145. add 999752 (0.953438)  time 0.074876 sec
  146. total time 0.407555 sec   search 2000000 loops found 1000469 (0.500235)
  147. End
  148. avp@avp-ubu1:hash_misc$ ./a.out 700000
  149. n = 700000 htsize = 1048576 (0.667572)
  150. step 1 hashing
  151. add 699897 (0.667474)  time 0.034560 sec
  152. total time 0.119492 sec   search 1400000 loops found 700246 (0.500176)
  153. hash * 2 + 1 step hashing
  154. add 699897 (0.667474)  time 0.032498 sec
  155. total time 0.115200 sec   search 1400000 loops found 700246 (0.500176)
  156. End
  157. avp@avp-ubu1:hash_misc$ ./a.out 100000
  158. n = 100000 htsize = 131072 (0.762939)
  159. step 1 hashing
  160. add 99997 (0.762917)  time 0.005376 sec
  161. total time 0.020578 sec   search 200000 loops found 100007 (0.500035)
  162. hash * 2 + 1 step hashing
  163. add 99997 (0.762917)  time 0.004863 sec
  164. total time 0.017239 sec   search 200000 loops found 100007 (0.500035)
  165. End
  166. avp@avp-ubu1:hash_misc$
  167. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement