-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbench.py
59 lines (45 loc) · 1.31 KB
/
bench.py
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
# Deduplication Benchmark
# If you have a fast, strong hash function for permutations
# Dont hesitate to open a PR
from functools import reduce
from time import process_time
from GAMS.generation import Individual
from GAMS.seeder import create_population
from GAMS.ranker import rank_perm
import numpy as np
import random
n = 100
dup = 9
a = create_population(n)
a = np.concatenate((a, random.choices(a, k=int(n * dup))))
b = [Individual(i, rank_perm(i)) for i in a]
# Test with default Hash function
def dedup(b): # Emulate our workload
ps = set()
for i in b:
if i not in ps:
ps.add(i)
return ps
def bench(b):
k = 100
t = 0
for i in range(k):
now = process_time()
dedup(b)
t += process_time() - now
return t / k
hl = list(bench(b) for _ in range(10))
hh = sum(hl) / 10
print(hh, max(hl) / min(hl), min(hl), max(hl))
def cantor_hash0(self: Individual):
return reduce(lambda a, b: (((a + b) * (a + b + 1)) >> 1) + b , map(int, self.data.flat))
def cantor_hash(self: Individual):
it = iter(map(int, self.data.flat))
a = next(it)
for i in it:
a = (((a + i) * (a + i + 1)) << 1) + i
return a
Individual.__hash__ = cantor_hash0
hl = list(bench(b) for _ in range(10))
hh = sum(hl) / 10
print(hh, max(hl) / min(hl), min(hl), max(hl))