-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patht0128.nim
93 lines (76 loc) · 1.79 KB
/
t0128.nim
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#[
SEE D20190624T151230
]#
import std/[hashes,oids,tables,strutils]
import std/math
import ../../murmur/murmur
const num2 = pow(2.float, 20).int - 1
proc computeEntropy[T](t: CountTable[T]): float =
var total = 0
var max = 0
for k,v in t:
total += v
if v > max: max = v
for k,v in t:
let x = v / total
assert x > 0
result -= x * log2(x)
# entropy of ideal, uniform distribution:
var y: float = 0
for k,v in t:
let x = 1 / t.len
y -= x * log2(x)
echo (y, total, total / t.len, max)
proc fun() =
var dummy: Hash
var t = CountTable[Hash]()
proc toHashMurmur3_2(a: string): Hash = toHashMurmur3(a)[0].Hash
template run(fun: typed) =
let n = 1_000_000
t.reset
for i in 0..<n:
let s = $genOid()
when false:
## bad cases
let s = align($i, 8)
let s = align($i, 9)
let s = align($i, 10)
let s = align($i, 11)
let s = align($i, 16)
let s = align($i, 17)
let s = align($i, 18)
let s = align($i, 19)
let s = $genOid()
when false:
## good cases
let s = align($i, 0)
let s = align($i, 1)
let s = align($i, 2)
let s = align($i, 3)
let s = align($i, 4)
let s = align($i, 5)
let s = align($i, 6)
let s = align($i, 7)
let s = align($i, 12)
let s = align($i, 13)
let s = align($i, 14)
let s = align($i, 15)
let s = align($i, 20)
let h = fun(s)
let h2 = h and num2
dummy += h2
t.inc h2
if i < 4:
echo (i, s, h, h2)
var count = 0
for k,v in t:
count.inc
if count < 4:
echo k, " ", v
echo (entropy: computeEntropy(t))
run toHashMurmur3_2
run hash
echo dummy
proc main()=
fun()
main()