-
Notifications
You must be signed in to change notification settings - Fork 3
/
benchmark.py
70 lines (52 loc) · 1.97 KB
/
benchmark.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
60
61
62
63
64
65
66
67
68
69
70
import numpy as np
from algorithms import algs
from costmatrices import generators
def get_results(num_it, shape, algs, generator):
times = []
results = []
for alg in algs:
if shape[0] != shape[1] and not alg.supports_rectangular:
times.append([float("inf")] * num_it)
results.append([float("inf")] * num_it)
else:
data = [alg().run(c) for c in generator(num_it, shape)]
dt, obj = zip(*data)
times.append(dt)
results.append(obj)
times = np.mean(times, axis=1)
errors = results - np.min(results, axis=0)
error_sums = np.sum(errors, axis=1)
return times, error_sums
def pretty_format(t, e):
missing = "---------"
if t == float("inf"):
return missing, missing
elif e == 0:
return ("%.2f" % (1000 * t)).rjust(9), "zero".rjust(9)
else:
return ("%.2f" % (1000 * t)).rjust(9), "%.3e" % e
def run():
num_it = 10
shapes = [(400, 400), (400, 800)]
for shape in shapes:
heading = "time (ms) error sum status"
if shape[0] == shape[1]:
print(("square tests: " + str(shape)).ljust(30) + heading)
else:
print(("retangular tests: " + str(shape)).ljust(30) + heading)
print("=" * 66 + "\n")
for generator in generators:
print(' ' * 4, generator.__name__)
times, errors = get_results(num_it, shape, algs, generator)
for alg, mtime, err in zip(algs, times, errors):
passed = mtime < float("inf") and err == 0
name = alg.__name__[4:]
tstr, estr = pretty_format(mtime, err)
status = ['fail', 'pass'][passed]
spaces = [8, 2, 8, 6, 0]
values = [name.ljust(20), tstr, estr, status]
line = ''.join([' ' * a + b for a, b in zip(spaces, values)])
print(line)
print()
if __name__ == "__main__":
run()