-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
61 lines (42 loc) · 1.49 KB
/
main.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
"""
CLI for benchmarking the different implementations.
"""
from datetime import datetime
from sys import argv, exit
import fsharp_implementation
import python_implementation
IMPLEMENTATIONS = {
"python": python_implementation.has_collided,
"fsharp": fsharp_implementation.has_collided
}
def build_projectile(x_pos):
return (x_pos - 1, 99), (x_pos + 1, 99), (x_pos - 1, 101), (x_pos + 1, 101)
def run_benchmark(iterations, test_implementation):
"""
Runs a speed benchmark on test_implementation.
:param iterations: The number of times to repeat each test
:param test_implementation: The callable for the the implementation to test
:return: Seconds elapsed
"""
target = ((195, 95), (205, 95), (205, 105), (195, 105))
start = datetime.now()
for count in range(0, iterations):
x_pos = 100
while not test_implementation(target, build_projectile(x_pos)):
x_pos += 1
return (datetime.now() - start).total_seconds()
def parse_args():
assert len(argv) == 3
return int(argv[1]), IMPLEMENTATIONS[argv[2]]
def main():
try:
iterations, implementation = parse_args()
except:
print("Usage: python3 main.py [iterations] [implementation]")
print("Valid implementations:", ", ".join(IMPLEMENTATIONS.keys()))
exit(1)
print("Running benchmark...")
result = run_benchmark(iterations, implementation)
print("Completed in {:.4f} seconds".format(result))
if __name__ == "__main__":
main()