Skip to content

Commit 50d4b40

Browse files
committed
Add fibonacci sample for benchmarks
1 parent 7a31c34 commit 50d4b40

6 files changed

Lines changed: 77 additions & 2 deletions

File tree

.claude/settings.local.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"mcp__github__list_issues",
1111
"Bash(gh issue:*)",
1212
"Bash(gh api:*)",
13-
"Bash(gh label:*)"
13+
"Bash(gh label:*)",
14+
"Bash(find /home/robin/projects/robin/pipe-lang/src/vm /home/robin/projects/robin/pipe-lang/src/interpreter -type f -name *.zig)"
1415
]
1516
}
1617
}

bench.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
N_RUNS="${1:-10}"
5+
PIPE="./zig-out/bin/pipe"
6+
FIB="samples/fibonacci.pipe"
7+
FIB_PY="samples/fibonacci.py"
8+
9+
echo "==> Building pipe..."
10+
zig build
11+
12+
echo ""
13+
echo "==> Verifying outputs match..."
14+
OUT_VM=$(${PIPE} "${FIB}")
15+
OUT_INTERP=$(${PIPE} --interp "${FIB}")
16+
OUT_PY=$(python3 "${FIB_PY}")
17+
18+
if [[ "${OUT_VM}" != "${OUT_INTERP}" || "${OUT_VM}" != "${OUT_PY}" ]]; then
19+
echo "ERROR: outputs differ!"
20+
echo " VM: ${OUT_VM}"
21+
echo " interp: ${OUT_INTERP}"
22+
echo " python: ${OUT_PY}"
23+
exit 1
24+
fi
25+
echo "All three produce: ${OUT_VM}"
26+
27+
echo ""
28+
if command -v hyperfine &>/dev/null; then
29+
echo "==> Benchmarking with hyperfine (${N_RUNS} runs each)..."
30+
hyperfine \
31+
--runs "${N_RUNS}" \
32+
--warmup 2 \
33+
--export-markdown bench-results.md \
34+
"${PIPE} ${FIB}" \
35+
"${PIPE} --interp ${FIB}" \
36+
"python3 ${FIB_PY}"
37+
echo ""
38+
echo "Results saved to bench-results.md"
39+
else
40+
echo "==> hyperfine not found — falling back to manual timing (${N_RUNS} runs each)..."
41+
echo ""
42+
43+
run_timed() {
44+
local label="$1"; shift
45+
local total=0
46+
for i in $(seq 1 "${N_RUNS}"); do
47+
local t
48+
t=$(TIMEFORMAT='%R'; { time "$@" > /dev/null; } 2>&1)
49+
total=$(awk "BEGIN { print ${total} + ${t} }")
50+
done
51+
local avg
52+
avg=$(awk "BEGIN { printf \"%.3f\", ${total} / ${N_RUNS} }")
53+
printf " %-40s avg %ss over %s runs\n" "${label}" "${avg}" "${N_RUNS}"
54+
}
55+
56+
run_timed "pipe VM (${FIB})" ${PIPE} "${FIB}"
57+
run_timed "pipe interp (${FIB})" ${PIPE} --interp "${FIB}"
58+
run_timed "python3 (${FIB_PY})" python3 "${FIB_PY}"
59+
fi

samples/fibonacci.pipe

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn fib(n: Int) Int {
2+
if n < 2 {
3+
return 1;
4+
}
5+
6+
return fib(n - 1) + fib(n - 2);
7+
}
8+
9+
print(fib(30));

samples/fibonacci.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def fib(n):
2+
if n < 2:
3+
return 1
4+
return fib(n - 1) + fib(n - 2)
5+
6+
print(fib(30))

src/main.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ fn runVm(source: []const u8, ctx: RuntimeContext, allocator: std.mem.Allocator)
3939
defer machine.deinit();
4040
try vm.builtins.registerAll(&machine.globals, allocator);
4141
_ = try machine.run();
42+
try ctx.writer.flush();
4243
}
4344

4445
fn runInterpreter(source: []const u8, ctx: RuntimeContext, allocator: std.mem.Allocator) !void {

src/type_checker.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,6 @@ pub const TypeChecker = struct {
408408
.int => .int,
409409
.string => .string,
410410
.null, .unit => .unit,
411-
412411
};
413412
return .{ .variable = .{ .pipe_type = pipe_type, .mutability = .constant } };
414413
}

0 commit comments

Comments
 (0)