summaryrefslogtreecommitdiff
path: root/ocaml/compare.sh
blob: cf2f90fcc4600719c0b790ca41ab1b9a9dd8d579 (plain)
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
#!/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."