tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

125 lignes · 4682 octetsBrut · Historique
1#!/usr/bin/env bash
2# Louise next to other languages on four small programs (tests/compare):
3# fib calls and integer arithmetic (naive Fibonacci of 35)
4# lists a map/filter/fold pipeline over a million integers, ten times
5# trees allocation: build and walk binary trees
6# floats float arithmetic in a loop, three million times
7# Each language's version prints the same number, which is checked. The
8# languages whose tools are missing are left out. Prints a Markdown table:
9# best wall-clock time of N runs, peak memory, and executable size for the
10# compiled ones (Louise release build, rustc -O, ghc -O2).
11#
12# scripts/compare.sh # N=3 (COMPARE_RUNS)
13#
14# Small programs, one machine: they show orders of magnitude, not a
15# ranking. Each round depends on its counter, so that no compiler computes
16# it only once; GHC still fuses `lists` into one loop that allocates nothing.
17set -euo pipefail
18cd "$(dirname "$0")/.."
19source scripts/lib.sh
20
21runs="${COMPARE_RUNS:-3}"
22build_release_louise
23out="target/compare"
24rm -rf "$out"
25mkdir -p "$out"
26src=tests/compare
27programs=(fib lists trees floats)
28have() { command -v "$1" >/dev/null; }
29
30# name: the command that runs program $1 (built into $out first).
31build() {
32 local lang=$1 p=$2
33 case $lang in
34 Louise) "$LOUISE_BIN" build --release "$src/$p.lse" -o "$out/lse_$p" >/dev/null ;;
35 Rust) rustc -O -C codegen-units=1 "$src/$p.rs" -o "$out/rs_$p" ;;
36 Go) go build -o "$out/go_$p" "$src/$p.go" ;;
37 Haskell) ghc -O2 -v0 -outputdir "$out/hs_$p.o" "$src/$p.hs" -o "$out/hs_$p" ;;
38 Java) javac -d "$out/java" "$src/${p^}.java" ;;
39 # Gleam compiles to JavaScript and runs on node (no Erlang in the mix).
40 Gleam)
41 local gdir="$out/gleam_$p"
42 rm -rf "$gdir"
43 mkdir -p "$gdir/src"
44 cp "$src/$p.gleam" "$gdir/src/main.gleam"
45 printf 'name = "compare"\n\n[dependencies]\ngleam_stdlib = ">= 0.44.0"\n' > "$gdir/gleam.toml"
46 (cd "$gdir" && gleam build --target=javascript >/dev/null)
47 printf 'import { main } from "./build/dev/javascript/compare/main.mjs";\nmain();\n' > "$gdir/run.mjs"
48 ;;
49 *) ;;
50 esac
51}
52command_of() {
53 local lang=$1 p=$2
54 case $lang in
55 Louise) echo "$out/lse_$p" ;;
56 Rust) echo "$out/rs_$p" ;;
57 Go) echo "$out/go_$p" ;;
58 Haskell) echo "$out/hs_$p" ;;
59 Java) echo "java -cp $out/java ${p^}" ;;
60 Gleam) echo "node $out/gleam_$p/run.mjs" ;;
61 Node) echo "node $src/$p.js" ;;
62 Python) echo "python3 $src/$p.py" ;;
63 esac
64}
65size_of() {
66 case $1 in
67 Louise) stat -c %s "$out/lse_$2" ;;
68 Rust) cp "$out/rs_$2" "$out/rs_$2.s" && strip "$out/rs_$2.s" && stat -c %s "$out/rs_$2.s" ;;
69 Go) cp "$out/go_$2" "$out/go_$2.s" && strip "$out/go_$2.s" && stat -c %s "$out/go_$2.s" ;;
70 Haskell) cp "$out/hs_$2" "$out/hs_$2.s" && strip "$out/hs_$2.s" && stat -c %s "$out/hs_$2.s" ;;
71 *) echo "" ;;
72 esac
73}
74
75langs=(Louise)
76have rustc && langs+=(Rust)
77have go && langs+=(Go)
78have ghc && langs+=(Haskell)
79have javac && have java && langs+=(Java)
80have gleam && have node && langs+=(Gleam)
81have node && langs+=(Node)
82have python3 && langs+=(Python)
83
84# The user-space instructions one run of the command counts (a stable
85# signal where wall time varies), or nothing without `perf`.
86instructions_of() {
87 have perf || { echo ""; return; }
88 # shellcheck disable=SC2086
89 perf stat --no-scale -x, -e instructions:u $1 >/dev/null 2>"$out/perf" || { echo ""; return; }
90 # A hybrid CPU splits the count over its core types; sum them.
91 awk -F, '$3 ~ /instructions/ && $1 ~ /^[0-9]+$/ { n += $1 } END { print n }' "$out/perf"
92}
93
94echo "| program | language | time | instructions | peak memory | executable (stripped) |"
95echo "|---|---|---:|---:|---:|---:|"
96for p in "${programs[@]}"; do
97 expected=""
98 for lang in "${langs[@]}"; do
99 build "$lang" "$p"
100 cmd=$(command_of "$lang" "$p")
101 best=""
102 mem=""
103 for _ in $(seq "$runs"); do
104 # shellcheck disable=SC2086
105 stats=$( { /usr/bin/time -f "%e %M" $cmd > "$out/output"; } 2>&1 | tail -1)
106 read -r t m <<< "$stats"
107 if [ -z "$best" ] || [ "$(echo "$t < $best" | bc)" = 1 ]; then
108 best=$t
109 mem=$m
110 fi
111 done
112 got=$(cat "$out/output")
113 if [ -z "$expected" ]; then
114 expected=$got
115 elif [ "$got" != "$expected" ]; then
116 echo "compare: $lang $p printed $got, expected $expected" >&2
117 exit 1
118 fi
119 instr=$(instructions_of "$cmd")
120 size=$(size_of "$lang" "$p")
121 printf "| %s | %s | %.2f s | %s | %d MB | %s |\n" "$p" "$lang" "$best" \
122 "${instr:+$(python3 -c "print(f'{$instr/1e9:.2f}G')" 2>/dev/null || echo "$instr")}" $((mem / 1024)) \
123 "${size:+$((size / 1024)) KB}"
124 done
125done