#!/usr/bin/env bash # Louise next to other languages on four small programs (tests/compare): # fib calls and integer arithmetic (naive Fibonacci of 35) # lists a map/filter/fold pipeline over a million integers, ten times # trees allocation: build and walk binary trees # floats float arithmetic in a loop, three million times # Each language's version prints the same number, which is checked. The # languages whose tools are missing are left out. Prints a Markdown table: # best wall-clock time of N runs, peak memory, and executable size for the # compiled ones (Louise release build, rustc -O, ghc -O2). # # scripts/compare.sh # N=3 (COMPARE_RUNS) # # Small programs, one machine: they show orders of magnitude, not a # ranking. Each round depends on its counter, so that no compiler computes # it only once; GHC still fuses `lists` into one loop that allocates nothing. set -euo pipefail cd "$(dirname "$0")/.." source scripts/lib.sh runs="${COMPARE_RUNS:-3}" build_release_louise out="target/compare" rm -rf "$out" mkdir -p "$out" src=tests/compare programs=(fib lists trees floats) have() { command -v "$1" >/dev/null; } # name: the command that runs program $1 (built into $out first). build() { local lang=$1 p=$2 case $lang in Louise) "$LOUISE_BIN" build --release "$src/$p.lse" -o "$out/lse_$p" >/dev/null ;; Rust) rustc -O -C codegen-units=1 "$src/$p.rs" -o "$out/rs_$p" ;; Go) go build -o "$out/go_$p" "$src/$p.go" ;; Haskell) ghc -O2 -v0 -outputdir "$out/hs_$p.o" "$src/$p.hs" -o "$out/hs_$p" ;; Java) javac -d "$out/java" "$src/${p^}.java" ;; # Gleam compiles to JavaScript and runs on node (no Erlang in the mix). Gleam) local gdir="$out/gleam_$p" rm -rf "$gdir" mkdir -p "$gdir/src" cp "$src/$p.gleam" "$gdir/src/main.gleam" printf 'name = "compare"\n\n[dependencies]\ngleam_stdlib = ">= 0.44.0"\n' > "$gdir/gleam.toml" (cd "$gdir" && gleam build --target=javascript >/dev/null) printf 'import { main } from "./build/dev/javascript/compare/main.mjs";\nmain();\n' > "$gdir/run.mjs" ;; *) ;; esac } command_of() { local lang=$1 p=$2 case $lang in Louise) echo "$out/lse_$p" ;; Rust) echo "$out/rs_$p" ;; Go) echo "$out/go_$p" ;; Haskell) echo "$out/hs_$p" ;; Java) echo "java -cp $out/java ${p^}" ;; Gleam) echo "node $out/gleam_$p/run.mjs" ;; Node) echo "node $src/$p.js" ;; Python) echo "python3 $src/$p.py" ;; esac } size_of() { case $1 in Louise) stat -c %s "$out/lse_$2" ;; Rust) cp "$out/rs_$2" "$out/rs_$2.s" && strip "$out/rs_$2.s" && stat -c %s "$out/rs_$2.s" ;; Go) cp "$out/go_$2" "$out/go_$2.s" && strip "$out/go_$2.s" && stat -c %s "$out/go_$2.s" ;; Haskell) cp "$out/hs_$2" "$out/hs_$2.s" && strip "$out/hs_$2.s" && stat -c %s "$out/hs_$2.s" ;; *) echo "" ;; esac } langs=(Louise) have rustc && langs+=(Rust) have go && langs+=(Go) have ghc && langs+=(Haskell) have javac && have java && langs+=(Java) have gleam && have node && langs+=(Gleam) have node && langs+=(Node) have python3 && langs+=(Python) # The user-space instructions one run of the command counts (a stable # signal where wall time varies), or nothing without `perf`. instructions_of() { have perf || { echo ""; return; } # shellcheck disable=SC2086 perf stat --no-scale -x, -e instructions:u $1 >/dev/null 2>"$out/perf" || { echo ""; return; } # A hybrid CPU splits the count over its core types; sum them. awk -F, '$3 ~ /instructions/ && $1 ~ /^[0-9]+$/ { n += $1 } END { print n }' "$out/perf" } echo "| program | language | time | instructions | peak memory | executable (stripped) |" echo "|---|---|---:|---:|---:|---:|" for p in "${programs[@]}"; do expected="" for lang in "${langs[@]}"; do build "$lang" "$p" cmd=$(command_of "$lang" "$p") best="" mem="" for _ in $(seq "$runs"); do # shellcheck disable=SC2086 stats=$( { /usr/bin/time -f "%e %M" $cmd > "$out/output"; } 2>&1 | tail -1) read -r t m <<< "$stats" if [ -z "$best" ] || [ "$(echo "$t < $best" | bc)" = 1 ]; then best=$t mem=$m fi done got=$(cat "$out/output") if [ -z "$expected" ]; then expected=$got elif [ "$got" != "$expected" ]; then echo "compare: $lang $p printed $got, expected $expected" >&2 exit 1 fi instr=$(instructions_of "$cmd") size=$(size_of "$lang" "$p") printf "| %s | %s | %.2f s | %s | %d MB | %s |\n" "$p" "$lang" "$best" \ "${instr:+$(python3 -c "print(f'{$instr/1e9:.2f}G')" 2>/dev/null || echo "$instr")}" $((mem / 1024)) \ "${size:+$((size / 1024)) KB}" done done