Skip to content

Commit 238ff0d

Browse files
committed
Improve figure format
1 parent 4353a23 commit 238ff0d

12 files changed

+74
-29
lines changed

.github/workflows/benchmark.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ on:
88

99
pull_request:
1010
branches: ["main"]
11+
12+
workflow_dispatch:
13+
1114
jobs:
1215
graph:
1316
runs-on: ${{ matrix.os }}

tools/benchmark.py

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,39 @@
2222

2323
from pathlib import Path
2424
import time
25-
from typing import List
25+
from typing import Callable, List, Tuple
2626
import random
2727
import subprocess
2828
import argparse
2929

3030
ROOT_PATH = Path(__file__).parent.parent
3131
BUILD_PATH_DEFAULT = "build"
3232

33+
FUNCTIONS = {
34+
"abs": lambda x, y: abs(x),
35+
"add": lambda x, y: x + y,
36+
"comparison": lambda x, y: x > y,
37+
"division": lambda x, y: x / y,
38+
"multiply": lambda x, y: x * y,
39+
"subtract": lambda x, y: x - y,
40+
"root": lambda x, y: x ** (1 / y),
41+
"power": lambda x, y: x**y,
42+
}
43+
44+
45+
def _benchmark_function(
46+
function: Callable,
47+
input_1: int,
48+
input_2: int,
49+
) -> float:
50+
start = time.time()
51+
try:
52+
function(input_1, input_2)
53+
except Exception as e:
54+
print(f"Error: {e}")
55+
end = time.time()
56+
return end - start
57+
3358

3459
def _benchmark(
3560
cmd: str,
@@ -60,21 +85,32 @@ def _benchmark(
6085
except subprocess.TimeoutExpired:
6186
print(f"Warning: Timeout running {cmd} on {input_str1}, {input_str2}")
6287
end = time.time()
63-
print(f"Time taken: {end - start}\033[A\r")
6488
return end - start
6589

6690

6791
def benchmark(
6892
cmd: str, inputs: List[str], limit: int, verbose: bool = False, timeout: int = 10
69-
) -> List[float]:
70-
random_number = str(random.randint(0, limit))
93+
) -> Tuple[List[float], List[float], bool]:
94+
random_number = random.randint(0, limit)
95+
random_number_str = str(random_number)
7196
time_needed = []
97+
time_needed_system = []
98+
system_available = False
7299
for input_str in inputs:
73100
time_needed.append(
74-
_benchmark(cmd, input_str, random_number, verbose=verbose, timeout=timeout)
101+
_benchmark(
102+
cmd, input_str, random_number_str, verbose=verbose, timeout=timeout
103+
)
75104
)
105+
if (func := Path(cmd).stem) in FUNCTIONS:
106+
time_needed_system.append(
107+
_benchmark_function(FUNCTIONS[func], int(input_str), random_number)
108+
)
109+
system_available = True
110+
else:
111+
time_needed_system.append(0)
76112

77-
return time_needed
113+
return time_needed, time_needed_system, system_available
78114

79115

80116
def main():
@@ -90,7 +126,7 @@ def main():
90126
"-e", "--executables", type=str, default="bin/*", required=False
91127
)
92128
parser.add_argument("-t", "--timeout", type=int, default=50, required=False)
93-
parser.add_argument("-l", "--limit", type=float, default=500, required=False)
129+
parser.add_argument("-l", "--limit", type=float, default=100, required=False)
94130
parser.add_argument("-v", "--verbose", action="store_true", required=False)
95131
parser.add_argument("-g", "--graph", action="store_true", required=False)
96132
args = parser.parse_args()
@@ -125,9 +161,11 @@ def main():
125161
print(f"Running benchmarks on {exe}")
126162
inputs_system = [i for i in range(LIMIT)]
127163
inputs = [str(i) for i in inputs_system]
128-
times = benchmark(
164+
time_result = benchmark(
129165
str(exe), inputs, LIMIT, verbose=args.verbose, timeout=args.timeout
130166
)
167+
times = time_result[0]
168+
times_system = time_result[1]
131169
if (not args.graph) and args.verbose:
132170
print(f"Times: {times}")
133171
if args.graph:
@@ -137,7 +175,11 @@ def main():
137175
import matplotlib.pyplot as plt
138176

139177
plt.figure(figsize=(15, 5))
140-
plt.plot(range(LIMIT), times)
178+
plt.plot(range(LIMIT), times, label="Steppable")
179+
if time_result[2]:
180+
plt.plot(range(LIMIT), times_system, label="System")
181+
182+
plt.legend()
141183
plt.xlabel("Input")
142184
plt.ylabel("Time (s)")
143185
plt.title(f"Benchmark for {exe.stem}")

tools/figures/benchmark_abs.svg

Lines changed: 2 additions & 2 deletions
Loading

tools/figures/benchmark_add.svg

Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading

tools/figures/benchmark_division.svg

Lines changed: 2 additions & 2 deletions
Loading

tools/figures/benchmark_multiply.svg

Lines changed: 2 additions & 2 deletions
Loading

tools/figures/benchmark_power.svg

Lines changed: 2 additions & 2 deletions
Loading

tools/figures/benchmark_root.svg

Lines changed: 2 additions & 2 deletions
Loading

tools/figures/benchmark_subtract.svg

Lines changed: 2 additions & 2 deletions
Loading

0 commit comments

Comments
 (0)