-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtimes.py
93 lines (72 loc) · 2.55 KB
/
runtimes.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
import os
import subprocess
import time
import statistics
import matplotlib.pyplot as plt
def measure_runtime(script_path, runs=10):
runtimes = []
for _ in range(runs):
start_time = time.time()
try:
subprocess.run(["python", script_path], check=True)
except subprocess.CalledProcessError as e:
print(f"Error running {script_path}: {e}")
return None
end_time = time.time()
runtime = end_time - start_time
runtimes.append(runtime)
return runtimes
def main():
root_directory = "./2022/" # Change this to your desired root directory
runs = 2 # Number of times to run each script
all_runtimes = {} # Dictionary to store script filenames and their runtimes
for root, dirs, files in os.walk(root_directory):
for file in files:
if file.endswith(".py"):
script_path = os.path.join(root, file)
runtimes = measure_runtime(script_path, runs)
if runtimes is not None:
all_runtimes[file] = runtimes
print(f"Script: {script_path}, Runtimes: {runtimes}")
plot_mean_runtime_bar_chart(all_runtimes)
def plot_mean_runtime_bar_chart(all_runtimes):
means = {
script: statistics.mean(runtimes) for script, runtimes in all_runtimes.items()
}
std_devs = {
script: statistics.stdev(runtimes) for script, runtimes in all_runtimes.items()
}
names = list(means.keys())
values = list(means.values())
std_dev_values = [std_devs[name] for name in names]
plt.figure(figsize=(10, 6))
bars = plt.bar(
names, values, color="orange", edgecolor="black", yerr=std_dev_values, capsize=5
)
for name, bar, std_dev in zip(names, bars, std_dev_values):
yval = bar.get_height()
# plt.text(
# bar.get_x() + bar.get_width() / 2,
# yval + 0.01,
# f"{yval:.3f}",
# ha="center",
# va="bottom",
# )
plt.text(
bar.get_x() + bar.get_width() / 2,
yval + 0.01 + std_dev,
f"{means[name]:.2f} s",
ha="center",
va="bottom",
)
plt.xlabel("Script Name")
plt.ylabel("Mean Runtime (seconds)")
plt.title(
f"Mean Runtimes with Standard Deviation (averaged over {len(all_runtimes[next(iter(all_runtimes))])} runs)"
)
plt.xticks(rotation=45, ha="right")
plt.grid(axis="y", linestyle="--", alpha=0.7)
plt.tight_layout()
plt.show()
if __name__ == "__main__":
main()