Skip to content

Commit fc51adb

Browse files
timfennisclaude
andcommitted
Add benchmarks, fix quicksort/sieve, update bench.sh for version comparison
Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
1 parent fa99f81 commit fc51adb

14 files changed

Lines changed: 279 additions & 47 deletions

File tree

bench.sh

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ echo "Building release binary..."
3030
cargo build --release --manifest-path "$SCRIPT_DIR/Cargo.toml" -q
3131

3232
echo ""
33-
echo "Validating the VM version can run '$FILE' without crashing..."
33+
echo "Validating versions can run '$FILE' without crashing..."
3434

3535
validate() {
3636
local label="$1"
@@ -42,14 +42,52 @@ validate() {
4242
echo " OK: $label"
4343
}
4444

45-
validate "ndc (PATH)" ndc run "$FILE"
45+
validate "ndc (PATH)" ndc run "$FILE"
4646
validate "local release (VM)" "$LOCAL" run "$FILE"
4747

48+
# ndc2 and ndc1 are optional: older versions may not support all language features
49+
# ndc1 also has a different CLI (no 'run' subcommand)
50+
USE_NDC2=false
51+
if command -v ndc2 &>/dev/null; then
52+
if ndc2 run "$FILE" &>/dev/null; then
53+
echo " OK: ndc2 (v0.2.1)"
54+
USE_NDC2=true
55+
else
56+
echo " SKIP: ndc2 (v0.2.1) failed to run '$FILE', skipping"
57+
fi
58+
else
59+
echo " SKIP: ndc2 not found, skipping"
60+
fi
61+
62+
USE_NDC1=false
63+
if command -v ndc1 &>/dev/null; then
64+
if ndc1 "$FILE" &>/dev/null; then
65+
echo " OK: ndc1 (v0.1.0)"
66+
USE_NDC1=true
67+
else
68+
echo " SKIP: ndc1 (v0.1.0) failed to run '$FILE', skipping"
69+
fi
70+
else
71+
echo " SKIP: ndc1 not found, skipping"
72+
fi
73+
4874
echo ""
4975
echo "Running benchmark..."
5076
echo ""
5177

78+
NDC2_ARGS=()
79+
if [[ "$USE_NDC2" == true ]]; then
80+
NDC2_ARGS=(--command-name "ndc2 (v0.2.1)" "ndc2 run '$FILE'")
81+
fi
82+
83+
NDC1_ARGS=()
84+
if [[ "$USE_NDC1" == true ]]; then
85+
NDC1_ARGS=(--command-name "ndc1 (v0.1.0)" "ndc1 '$FILE'")
86+
fi
87+
5288
hyperfine \
5389
--warmup 3 \
5490
--command-name "ndc (installed)" "ndc run '$FILE'" \
55-
--command-name "local release (VM)" "'$LOCAL' run '$FILE'"
91+
--command-name "local release (VM)" "'$LOCAL' run '$FILE'" \
92+
"${NDC2_ARGS[@]}" \
93+
"${NDC1_ARGS[@]}"

benches/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ rand_chacha.workspace = true
1515
name = "benchmark"
1616
path = "src/benchmark.rs"
1717
harness = false
18+
19+
[[bench]]
20+
name = "output_sink"
21+
path = "src/output_sink.rs"
22+
harness = false

benches/programs/ackermann.ndc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn ackermann(m, n) {
2+
if m == 0 { n + 1 }
3+
else if n == 0 { ackermann(m - 1, 1) }
4+
else { ackermann(m - 1, ackermann(m, n - 1)) }
5+
}
6+
7+
print(ackermann(3, 7));

benches/programs/bigint.ndc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let result = 1;
2+
for i in 2..=5000 {
3+
result *= i;
4+
}
5+
print(result.string.len, "digits");

benches/programs/closures.ndc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let adders = [fn(x) => x + i for i in 0..10_000];
2+
let result = adders.map(fn(f) => f(0)).sum;
3+
print(result);

benches/programs/hof_pipeline.ndc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let result = list(1..=100_000)
2+
.filter(fn(x) => x % 2 == 0)
3+
.map(fn(x) => x * x)
4+
.reduce(fn(acc, x) => acc + x);
5+
print(result);

benches/programs/map_ops.ndc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let m = %{};
2+
for i in 1..=50_000 {
3+
let key = (i * i) % 997;
4+
if key in m {
5+
m[key] += 1;
6+
} else {
7+
m[key] = 1;
8+
}
9+
}
10+
let most_common = m.keys.max_by_key(fn(k) => m[k]);
11+
print("Unique keys:", m.keys.len, "Most common:", most_common, "count:", m[most_common]);

benches/programs/matrix_mul.ndc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn mat_mul(a, b, n) {
2+
[[fold(0..n, 0, fn(acc, k) => acc + a[r][k] * b[k][c]) for c in 0..n] for r in 0..n]
3+
}
4+
5+
let n = 50;
6+
let a = [[(r + c) % 7 for c in 0..n] for r in 0..n];
7+
let b = [[(r * c + 1) % 5 for c in 0..n] for r in 0..n];
8+
let result = mat_mul(a, b, n);
9+
print(result[0][0]);

benches/programs/print_heavy.ndc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
for i in 0..10_000 {
2+
print(i);
3+
}

benches/programs/quicksort.ndc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ fn quicksort(arr) {
22
if arr.len <= 1 { return arr };
33
let pivot = arr[0];
44
let left = arr.filter(fn(x) => x < pivot);
5+
let mid = arr.filter(fn(x) => x == pivot);
56
let right = arr.filter(fn(x) => x > pivot);
6-
return quicksort(left) ++ [pivot] ++ quicksort(right);
7+
return quicksort(left) ++ mid ++ quicksort(right);
78
}
89

910
print(quicksort([

0 commit comments

Comments
 (0)