Advertisement
Guest User

Untitled

a guest
Mar 27th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.36 KB | None | 0 0
  1. /* By Paul Hsieh (C) 2004, 2005.  Covered under the Paul Hsieh derivative
  2.    license. See:
  3.    http://www.azillionmonkeys.com/qed/weblicense.html for license details.
  4.  
  5.    http://www.azillionmonkeys.com/qed/hash.html */
  6. #include <stdint.h>
  7. #define NULL (void*)0
  8. #undef get16bits
  9. #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
  10.   || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
  11. #define get16bits(d) (*((const uint16_t *) (d)))
  12. #endif
  13.  
  14. #if !defined (get16bits)
  15. #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
  16.                        +(uint32_t)(((const uint8_t *)(d))[0]) )
  17. #endif
  18.  
  19. uint32_t SuperFastHash (const char * data, int len) {
  20.   uint32_t hash = len, tmp;
  21.   int rem;
  22.  
  23.   if (len <= 0 || data == NULL) return 0;
  24.  
  25.   rem = len & 3;
  26.   len >>= 2;
  27.  
  28.   /* Main loop */
  29.   for (;len > 0; len--) {
  30.     hash  += get16bits (data);
  31.     tmp    = (get16bits (data+2) << 11) ^ hash;
  32.     hash   = (hash << 16) ^ tmp;
  33.     data  += 2*sizeof (uint16_t);
  34.     hash  += hash >> 11;
  35.   }
  36.  
  37.   /* Handle end cases */
  38.   switch (rem) {
  39.     case 3: hash += get16bits (data);
  40.             hash ^= hash << 16;
  41.             hash ^= ((signed char)data[sizeof (uint16_t)]) << 18;
  42.             hash += hash >> 11;
  43.             break;
  44.     case 2: hash += get16bits (data);
  45.             hash ^= hash << 11;
  46.             hash += hash >> 17;
  47.             break;
  48.     case 1: hash += (signed char)*data;
  49.             hash ^= hash << 10;
  50.             hash += hash >> 1;
  51.   }
  52.  
  53.   /* Force "avalanching" of final 127 bits */
  54.   hash ^= hash << 3;
  55.   hash += hash >> 5;
  56.   hash ^= hash << 4;
  57.   hash += hash >> 17;
  58.   hash ^= hash << 25;
  59.   hash += hash >> 6;
  60.  
  61.   return hash;
  62. }
  63.  
  64. /* Benchmark */
  65.  
  66. #include <time.h>
  67. #include <stdio.h>
  68. #include <unistd.h>
  69.  
  70. int main(void) {
  71.   struct timespec start, end;
  72.   long secs;
  73.   long hashes = 0;
  74.   char data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
  75.   clock_gettime(CLOCK_MONOTONIC, &start);
  76.   clock_gettime(CLOCK_MONOTONIC, &end);
  77.   while ((secs = end.tv_sec - start.tv_sec) < 20) {
  78.     uint32_t hash = SuperFastHash(data, 20);
  79.     data[hash % 20] += 1;
  80.     clock_gettime(CLOCK_MONOTONIC, &end);
  81.     ++hashes;
  82.   }
  83.  
  84.   printf("secs: %ld, hashes: %ld, hashes/sec: %f, Khashes/sec: %f\n", secs, hashes, hashes/20.0,
  85.       hashes/20.0/1000.0);
  86.   return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement