| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
import sys |
| 28 |
|
| 29 |
WORDS = [] |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
def hash_dillon(s): |
| 36 |
hash = 0 |
| 37 |
for c in s: |
| 38 |
hash = (hash << 2) ^ ord(c) |
| 39 |
return hash |
| 40 |
|
| 41 |
def hash_djb1(s): |
| 42 |
hash = 5381 |
| 43 |
for c in s: |
| 44 |
hash = ((hash << 5) + hash) + ord(c) |
| 45 |
return hash |
| 46 |
|
| 47 |
def hash_djb2(s): |
| 48 |
hash = 5381 |
| 49 |
for c in s: |
| 50 |
hash = ((hash << 5) + hash) ^ ord(c) |
| 51 |
return hash |
| 52 |
|
| 53 |
def hash_sdbm(s): |
| 54 |
hash = 0 |
| 55 |
for c in s: |
| 56 |
hash = ord(c) + (hash << 6) + (hash << 16) - hash |
| 57 |
return hash |
| 58 |
|
| 59 |
def hash_silly(s): |
| 60 |
hash = 0 |
| 61 |
for c in s: |
| 62 |
hash = hash + ord(c) |
| 63 |
return hash |
| 64 |
|
| 65 |
def hash_silly2(s): |
| 66 |
hash = 0 |
| 67 |
i = 1 |
| 68 |
for c in s: |
| 69 |
hash = hash + i*ord(c) |
| 70 |
i += 1 |
| 71 |
return hash |
| 72 |
|
| 73 |
def hash_silly3(s): |
| 74 |
hash = 0 |
| 75 |
i = 1 |
| 76 |
for c in s: |
| 77 |
|
| 78 |
|
| 79 |
hash = hash + i**2*ord(c)**3 |
| 80 |
i += 1 |
| 81 |
return hash |
| 82 |
|
| 83 |
def hash_knuth(s): |
| 84 |
hash = len(s) |
| 85 |
for c in s: |
| 86 |
hash = ((hash<<5)^(hash>>27))^ord(c) |
| 87 |
return hash |
| 88 |
|
| 89 |
|
| 90 |
def hash_kankowski(s): |
| 91 |
hash = 0 |
| 92 |
for c in s: |
| 93 |
hash = 17 * hash + (ord(c) - ord(' ')); |
| 94 |
return hash ^ (hash >> 16); |
| 95 |
|
| 96 |
def eval_hash(hash_func): |
| 97 |
d = {} |
| 98 |
for w in WORDS: |
| 99 |
h = hash_func(w) |
| 100 |
d[h] = d.get(h, 0) + 1 |
| 101 |
return d |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
def compress_dict(d, size): |
| 108 |
c = {} |
| 109 |
for k in d: |
| 110 |
n = k % size |
| 111 |
c[n] = c.get(n, 0) + d[k] |
| 112 |
return c |
| 113 |
|
| 114 |
def print_eval(d,s): |
| 115 |
print "------%s-----" % (s) |
| 116 |
print "Unique hash values: %s" % (len(d)) |
| 117 |
print "Max collisions: %s" % (max(d.values())) |
| 118 |
print "Min collisions: %s" % (min(d.values())) |
| 119 |
print "Avg collisions: %s" % (sum(d.values())/float(len(d))) |
| 120 |
print "Med collisions: %s" % (sorted(d.values())[len(d)/2]) |
| 121 |
|
| 122 |
|
| 123 |
f = open(sys.argv[1]) |
| 124 |
for l in f: |
| 125 |
l = l.strip() |
| 126 |
WORDS.append(l) |
| 127 |
f.close() |
| 128 |
|
| 129 |
FUNCS = [hash_dillon, hash_djb1, hash_djb2, hash_sdbm, |
| 130 |
hash_silly, hash_silly2, hash_silly3, |
| 131 |
hash_knuth, hash_kankowski] |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
for f in FUNCS: |
| 136 |
print "========== %s ==========" % f.__name__ |
| 137 |
|
| 138 |
d = eval_hash(f) |
| 139 |
print_eval(d,"raw") |
| 140 |
|
| 141 |
c = compress_dict(d, 4096) |
| 142 |
print_eval(c,"4096") |
| 143 |
|
| 144 |
c = compress_dict(d, 1024) |
| 145 |
print_eval(c,"1024") |
| 146 |
|