-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmarks.py
80 lines (67 loc) · 2.62 KB
/
benchmarks.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
71
72
73
74
75
76
77
78
79
80
from datetime import datetime
from sysconfig import get_python_version
from timeit import Timer
''' The reason that this particular combination of runs was chosen is
not meant to be obvious.
A standard set of runs were designed to test the mazelib library
in the most typical use-case of the target audience.
The only really important part of this benchmark is that there is a
standard basis for comparison.
'''
SIZES = [5, 10, 25, 50, 100, 200]
ITERATIONS = [100, 100, 100, 50, 1, 1]
GENERATORS = ['AldousBroder', 'AldousBroder',
'BacktrackingGenerator', 'BacktrackingGenerator',
'BinaryTree', 'BinaryTree',
'Division', 'Division',
'GrowingTree', 'GrowingTree',
'HuntAndKill', 'HuntAndKill',
'Prims', 'Prims',
'Sidewinder', 'Sidewinder',
'TrivialMaze', 'TrivialMaze',
'Wilsons', 'Wilsons']
SOLVERS = ['Collision', 'WallFollower'] * int(len(GENERATORS) / 2)
def main():
times = run_benchmarks()
print_benchmarks(times)
def run_benchmarks():
''' Run the benchmarks.
An annoying screen-print will occur so that you know your
progress, as these tests might take a while.
'''
times = [[0.0]*len(SIZES) for _ in range(len(GENERATORS))]
for row,generator in enumerate(GENERATORS):
solver = SOLVERS[row]
print('Run #%d: %s & %s' % (row, generator, solver))
for col,size in enumerate(SIZES):
print(col)
setup = """from mazelib import Maze
from mazelib.solve.%(solv)s import %(solv)s
from mazelib.generate.%(gen)s import %(gen)s
""" % {'solv': solver, 'gen': generator}
logic = """m = Maze()
m.generator = %(gen)s(%(size)d, %(size)d)
m.solver = %(solv)s()
m.generate()
m.generate_entrances()
m.solve()
""" % {'solv': solver, 'gen': generator, 'size': size}
t = Timer(logic, setup=setup)
time = t.timeit(ITERATIONS[col])
times[row][col] = time
return times
def print_benchmarks(times):
''' Pretty print for the benchmark results,
with a detailed CSV at the end.
'''
print('\nmazelib benchmarking')
print(datetime.now().strftime('%Y-%m-%d %H:%M'))
print('Python version: ' + get_python_version())
print('\nTotal Time (seconds): %.5f\n' %
sum([sum(times_row) for times_row in times]))
print('\nmaze size,' + ','.join([str(s) for s in SIZES]))
for row in range(len(times)):
method = GENERATORS[row] + '-' + SOLVERS[row] + ','
print(method + ','.join(['%.5f' % time for time in times[row]]))
if __name__ == '__main__':
main()