-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbnb.py
More file actions
125 lines (102 loc) Β· 4.11 KB
/
bnb.py
File metadata and controls
125 lines (102 loc) Β· 4.11 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import os
import shutil
import subprocess
import time
from pathlib import Path
import sys
import re
root = Path(__file__).parent.resolve()
dist = root / "dist"
example_main = root / "example/main.py"
results_file = root / "RESULTS.md"
def run_capture(cmd, desc=""):
print(f"βΆ Running: {' '.join(cmd)} {desc}")
start = time.perf_counter()
result = subprocess.run(
cmd,
capture_output=True,
text=True,
encoding="utf-8",
errors="replace"
)
output = (result.stdout or "") + ("\n" + (result.stderr or ""))
duration = time.perf_counter() - start
print(f"β
Finished {desc} in {duration:.4f}s\n")
return output, duration
def clean_folders():
for folder in ("dist", "build", ".hatch"):
f = root / folder
if f.exists():
shutil.rmtree(f)
def build_and_install():
run_capture([sys.executable, "-m", "hatch", "build"], "Building wheel")
wheels = list(dist.glob("*.whl"))
if not wheels:
raise RuntimeError("β No wheel found in dist folder!")
latest_wheel = max(wheels, key=os.path.getmtime)
run_capture([sys.executable, "-m", "pip", "install", "--upgrade", str(latest_wheel)], "Installing wheel")
def fix_boosted_file(path: Path):
boosted = path.with_name(path.stem + "_autonumba.py")
if not boosted.exists():
print(f"βΆ Generating boosted file {boosted.name}...")
# disable Rich/emoji output on Windows
run_capture([sys.executable, "-m", "autonumba", str(path), "--no-rich"], "Boosting original script")
if not boosted.exists():
raise FileNotFoundError(f"β Boosted file still missing: {boosted}")
return boosted
def parse_activities(output):
activities = {}
patterns = [
r"Matrix multiply done in ([0-9.]+) seconds",
r"Fibonacci done in ([0-9.]+) seconds",
r"Heavy loop done in ([0-9.]+) seconds"
]
names = ["Matrix multiply", "Fibonacci", "Heavy loop"]
for name, pat in zip(names, patterns):
match = re.search(pat, output)
activities[name] = float(match.group(1)) if match else None
return activities
def write_results_md(timings, outputs):
md = "# π autonumba Benchmark Results per Activity\n\n"
activities = ["Matrix multiply", "Fibonacci", "Heavy loop"]
md += "| Activity | Original π (s) | Boosted π₯ (s) | Faster % | Speed-up | Winner |\n"
md += "|----------|----------------|----------------|-----------|----------|--------|\n"
for act in activities:
orig_time = timings["Original π"].get(act)
boost_time = timings["Boosted π₯"].get(act)
if orig_time and boost_time:
faster_percent = ((orig_time - boost_time) / orig_time) * 100
speedup = orig_time / boost_time
else:
faster_percent, speedup = "-", "-"
winner = "Original π"
if boost_time and orig_time and boost_time < orig_time:
winner = "Boosted π₯"
orig_str = f"{orig_time:.4f}" if orig_time else "-"
boost_str = f"{boost_time:.4f}" if boost_time else "-"
faster_str = f"{faster_percent:.2f}%" if faster_percent != "-" else "-"
speedup_str = f"{speedup:.2f}x" if speedup != "-" else "-"
md += f"| {act} | {orig_str} | {boost_str} | {faster_str} | {speedup_str} | {winner} |\n"
md += "\n"
for name in ["Original π", "Boosted π₯"]:
out = outputs.get(name, "")
md += f"## π {name} Output\n```\n{out}\n```\n\n"
results_file.write_text(md, encoding="utf-8")
print("π RESULTS.md written with per-activity benchmark summary.")
def main():
clean_folders()
build_and_install()
boosted_main_fixed = fix_boosted_file(example_main)
runs = [
("Original π", [sys.executable, "-u", str(example_main)]),
("Boosted π₯", [sys.executable, "-u", str(boosted_main_fixed)]),
]
timings = {}
outputs = {}
for name, cmd in runs:
out, _ = run_capture(cmd, desc=name)
outputs[name] = out
timings[name] = parse_activities(out)
write_results_md(timings, outputs)
if __name__ == "__main__":
main()