Debian Codegraph @

research/compare_hashes.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 #
4 # Experiment to compare various basic hash functions.
5 #
6 # Peter H. Froehlich <phf at acm dot org>
7
8 # I wrote this to play with hash functions since DASM's hash1
9 # looked suspicious (and it was also duplicated in the code?).
10 # It turns out that it's not that bad for hashing our opcodes,
11 # but Dan Bernstein's hash does twice as well for no real cost
12 # increase, so I'll probably use Dan's instead of Matt's.
13 #
14 # Note that perfect hashing is tempting since we know the keys
15 # in advance, they don't change dynamically (for opcodes!). So
16 # I gave it some thought (i.e. wasted some brain cycles) before
17 # realizing that DASM likes to "mix and match" tables, i.e. it
18 # produces bigger compound tables from smaller ones based on
19 # what processor is selected. So if we want to preserve that
20 # aspect of DASM's design, we can't have perfect hashing. :-/
21 #
22 # Just as well, I have a feeling that binary search on "static"
23 # arrays would do even better, improved locality of reference
24 # and all that. But I want to keep the hashes, they are a big
25 # thing in DASM. :-)
26
27 import sys
28
29 WORDS = []
30
31 # We use "raw" hash functions without a compression step; if we
32 # want to evaluate for a given table size, we compress results
33 # later.
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) # hash * 33 + 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 # these work pretty well for some reason (for opcodes anyway)
78 #hash = hash + i**1*ord(c)**3
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 # http://smallcode.weblogs.us/2008/01/22/hash-functions-an-empirical-comparison/
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 # Note that DASM uses & for compression; % is the same for positive
104 # numbers (*unsigned* anyway in DASM), but it could cost a few more
105 # cycles.
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 # read file into WORDS list, assumes one word per line
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 # TODO: generate some nice graphics with PyX?
134
135 for f in FUNCS:
136 print "========== %s ==========" % f.__name__
137 # just the hash code please
138 d = eval_hash(f)
139 print_eval(d,"raw")
140 # Matt's 2.16 DASM uses 4096 for symbols, 1024 for mnemonics
141 c = compress_dict(d, 4096)
142 print_eval(c,"4096")
143 # our DASM uses 1024 for both hash tables right now...
144 c = compress_dict(d, 1024)
145 print_eval(c,"1024")
146