forked from john-science/mazelib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmarks.py
95 lines (81 loc) · 2.65 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
""" The benchmarks below are useful for testing performance when making changes to the maze algorithms. """
from datetime import datetime
from sysconfig import get_python_version
from timeit import Timer
from mazelib import __version__ as version
# CONFIG
SIZES = [5, 10, 25, 50, 100]
ITERATIONS = [100, 50, 20, 5, 1]
GENERATORS = [
"AldousBroder",
"BacktrackingGenerator",
"BinaryTree",
"HuntAndKill",
"Prims",
"Sidewinder",
"TrivialMaze",
"Wilsons",
]
SOLVERS = ["Collision", "Tremaux"]
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.
Returns:
list: 2D list of the team each generator/solver combination took
"""
times = [[0.0] * len(SIZES) for _ in range(len(GENERATORS) * len(SOLVERS))]
row = -1
for generator in GENERATORS:
for solver in SOLVERS:
row += 1
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]
times[row][col] = time
return times
def print_benchmarks(times):
"""Pretty print for the benchmark results, with a detailed CSV at the end.
Args:
times (list): timing results for the benchmark runs
Results: None
"""
print("\nmazelib benchmarking")
print(datetime.now().strftime("%Y-%m-%d %H:%M"))
print("Python version: {0}".format(get_python_version()))
print("mazelib version: {0}".format(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]))
row = -1
for generator in GENERATORS:
for solver in SOLVERS:
row += 1
method = generator + "-" + solver + ","
print(method + ",".join(["%.5f" % time for time in times[row]]))
if __name__ == "__main__":
main()