summaryrefslogtreecommitdiff
path: root/ocaml/compare.sh
diff options
context:
space:
mode:
authorpolwex <polwex@sortug.com>2025-10-05 21:56:51 +0700
committerpolwex <polwex@sortug.com>2025-10-05 21:56:51 +0700
commitfcedfddf00b3f994e4f4e40332ac7fc192c63244 (patch)
tree51d38e62c7bdfcc5f9a5e9435fe820c93cfc9a3d /ocaml/compare.sh
claude is gud
Diffstat (limited to 'ocaml/compare.sh')
-rwxr-xr-xocaml/compare.sh81
1 files changed, 81 insertions, 0 deletions
diff --git a/ocaml/compare.sh b/ocaml/compare.sh
new file mode 100755
index 0000000..cf2f90f
--- /dev/null
+++ b/ocaml/compare.sh
@@ -0,0 +1,81 @@
+#!/bin/bash
+
+echo "=========================================="
+echo "Nock Interpreter Benchmark Comparison"
+echo "=========================================="
+echo ""
+
+echo "Running Simple C Implementation..."
+echo "------------------------------------------"
+./bench_simple > /tmp/bench_c.txt 2>&1
+cat /tmp/bench_c.txt
+echo ""
+
+echo "Running OCaml Implementation..."
+echo "------------------------------------------"
+dune exec ./bench_nock.exe > /tmp/bench_ocaml.txt 2>&1
+cat /tmp/bench_ocaml.txt
+echo ""
+
+echo "=========================================="
+echo "Side-by-Side Comparison"
+echo "=========================================="
+echo ""
+echo "Note: Simple C uses a minimal interpreter without Vere's infrastructure"
+echo "OCaml uses Zarith (GMP-based) for arbitrary-precision integers"
+echo ""
+
+# Extract ops/sec and create comparison
+echo "Operation | C (ops/sec) | OCaml (ops/sec) | Ratio (C/OCaml)"
+echo "-------------------------------|--------------|-----------------|----------------"
+
+extract_ops() {
+ grep "$1" /tmp/bench_c.txt | awk '{print $(NF-1)}' | tr -d ','
+}
+
+extract_ops_ocaml() {
+ grep "$1" /tmp/bench_ocaml.txt | awk '{print $(NF-1)}' | tr -d ','
+}
+
+compare_bench() {
+ name="$1"
+ c_ops=$(extract_ops "$name")
+ ocaml_ops=$(extract_ops_ocaml "$name")
+
+ if [ -n "$c_ops" ] && [ -n "$ocaml_ops" ]; then
+ ratio=$(echo "scale=2; $c_ops / $ocaml_ops" | bc)
+ printf "%-30s | %12.0f | %15.0f | %5.2fx\n" "$name" "$c_ops" "$ocaml_ops" "$ratio"
+ fi
+}
+
+compare_bench "Opcode 0: slot"
+compare_bench "Opcode 1: constant"
+compare_bench "Opcode 3: is-cell"
+compare_bench "Opcode 4: increment"
+compare_bench "Opcode 5: equality"
+compare_bench "Opcode 6: if-then-else"
+compare_bench "Opcode 7: composition"
+compare_bench "Opcode 8: push"
+compare_bench "Cell construction"
+compare_bench "Deep slot lookup"
+
+echo ""
+echo "=========================================="
+echo "Summary"
+echo "=========================================="
+echo ""
+echo "The simple C implementation is faster primarily because:"
+echo " 1. No garbage collection overhead"
+echo " 2. Direct pointer manipulation"
+echo " 3. Inline function calls with -O3"
+echo " 4. Stack allocation for small values"
+echo ""
+echo "The OCaml implementation advantages:"
+echo " 1. Memory safety (no manual memory management)"
+echo " 2. Arbitrary-precision integers built-in (via Zarith/GMP)"
+echo " 3. Pattern matching for cleaner code"
+echo " 4. Type safety catches errors at compile time"
+echo ""
+echo "For a real comparison with Vere, we would need to benchmark"
+echo "against the full Vere nock interpreter with its loom allocator,"
+echo "jet system, and memoization."