-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestHashFunction.py
More file actions
69 lines (55 loc) · 1.68 KB
/
testHashFunction.py
File metadata and controls
69 lines (55 loc) · 1.68 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import json
import os, sys
import time
blue = "\x1b[34m"
red = "\x1b[31m"
green = "\x1b[32m"
white = "\x1b[37m"
yellow = "\x1b[33m"
cyan = "\x1b[36m"
reset = "\x1b[0m"
def load_words():
try:
with open("words_dictionary.json", "r") as english_dictionary:
valid_words = json.load(english_dictionary)
return valid_words
except Exception as e:
return str(e)
def testHash(hashFn):
timeStart()
words = list(load_words().keys())
timeStop("load words")
print("You are about to work on ", green, len(words), reset, " words")
results = {}
timeStart()
for word in words:
hash = hashFn(word)
if hash in results:
results[hash].append(word)
else:
results[hash] = [word]
timeStop("iterate over words")
if len(results) < len(words):
for hash, innerWords in results.items():
if len(innerWords) > 1:
maxFirstTen = innerWords[:10]
s = ", ".join([str(v) for v in maxFirstTen])
print(
"Found: ", white,
s,
", ..." if len(innerWords) > 10 else "",
reset,
" for ", cyan, hash, reset,
" so ", red, len(innerWords), reset, "collisions")
print("There was a total of ", red, len(words) - len(results), reset, "collisions")
else:
print(green, "WOW, no collision", reset)
startTime = 0
endTime = 0
def timeStart():
global startTime
startTime = time.time()
def timeStop(key):
global endTime
endTime = time.time()
print(key, "took: ", blue, (endTime - startTime) * 1000, reset)