Self-timing benchmark

fib(27), a 2-million iteration loop, and a 200k-element vector build, each reporting its own elapsed milliseconds.

PatLang source
# Self-timing benchmark in the Stage 1 self-hosted language.
# Reports its own elapsed time via now_ms(), so the same program measures
# itself natively, on WASM, and under the interpreter.

make a function called fib takes n returns r
  if n < 2 then
    return n
  else
    return fib(n - 1) + fib(n - 2)
  end
end

make a function called sum_to takes n returns total
  let total = 0
  let i = 1
  while i <= n do
    let total = total + i
    let i = i + 1
  end
  return total
end

let t0 = now_ms()
let f = fib(27)
let t1 = now_ms()
print("fib(27) = " + f + "  [" + (t1 - t0) + " ms]")

let t2 = now_ms()
let s = sum_to(2000000)
let t3 = now_ms()
print("sum 1..2000000 = " + s + "  [" + (t3 - t2) + " ms]")

let t4 = now_ms()
let xs = vec_new()
let k = 0
while k < 200000 do
  vec_push(xs, k * 2)
  let k = k + 1
end
let t5 = now_ms()
print("built vector of " + vec_len(xs) + "  [" + (t5 - t4) + " ms]")
print("total: " + (t5 - t0) + " ms")

(not run yet)

Native run on the build machine:

fib(27) = 196418  [217 ms]
sum 1..2000000 = 2000001000000  [563 ms]
built vector of 200000  [68 ms]
total: 848 ms