// Optimized variant 1: faster loops without changing benchmark outputs.
// Keeps the same BENCH output format as bench.ling.
fn fib_iter(n: number) -> number {
bind a = 0
bind b = 1
bind i = 0
while i < n {
bind t = a + b
bind a = b
bind b = t
bind i = i + 1
}
a
}
bind start = do {
bind PI = 3.141592653589793
// 1. fib
bind t0 = time_now()
bind r = fib_iter(30)
print("BENCH fib RESULT ", r, " TIME ", time_now() - t0)
// 2. loop_sum (7-step chunking)
bind t0 = time_now()
bind N = 10000000
bind s = 0
bind i = 0
while i + 7 <= N {
bind s = s + 21
bind i = i + 7
}
while i < N {
bind s = s + (i % 7)
bind i = i + 1
}
print("BENCH loop_sum RESULT ", s, " TIME ", time_now() - t0)
// 3. leibniz (baseline shape)
bind t0 = time_now()
bind acc = 0.0
bind k = 0
bind sign = 1.0
while k < 5000000 {
bind acc = acc + sign / (2.0 * k + 1.0)
bind sign = 0.0 - sign
bind k = k + 1
}
bind piapprox = 4.0 * acc
print("BENCH leibniz RESULT ", piapprox, " TIME ", time_now() - t0)
// 4. primes (baseline)
bind t0 = time_now()
bind count = 0
bind n = 2
while n < 50000 {
bind d = 2
bind is_p = 1
while d * d <= n {
if n % d == 0 {
bind is_p = 0
bind d = n
}
bind d = d + 1
}
bind count = count + is_p
bind n = n + 1
}
print("BENCH primes RESULT ", count, " TIME ", time_now() - t0)
// 5. mandelbrot (reduced repeated division)
bind t0 = time_now()
bind W = 200
bind H = 200
bind maxiter = 100
bind x_step = 3.5 / W
bind y_step = 2.0 / H
bind total = 0
bind py = 0
bind y0 = -1.0
while py < H {
bind px = 0
bind x0 = -2.5
while px < W {
bind zx = 0.0
bind zy = 0.0
bind it = 0
while zx * zx + zy * zy <= 4.0 && it < maxiter {
bind xt = zx * zx - zy * zy + x0
bind zy = 2.0 * zx * zy + y0
bind zx = xt
bind it = it + 1
}
bind total = total + it
bind x0 = x0 + x_step
bind px = px + 1
}
bind y0 = y0 + y_step
bind py = py + 1
}
print("BENCH mandelbrot RESULT ", total, " TIME ", time_now() - t0)
// 6. fm_synth (remove per-iter division)
bind t0 = time_now()
bind N = 1000000
bind sr = 44100.0
bind dt = 1.0 / sr
bind w1 = 2.0 * PI * 220.0
bind w2 = 2.0 * PI * 440.0
bind sum = 0.0
bind j = 0
bind t = 0.0
while j < N {
bind sample = sin(w1 * t + sin(w2 * t))
bind sum = sum + sample
bind t = t + dt
bind j = j + 1
}
print("BENCH fm_synth RESULT ", sum, " TIME ", time_now() - t0)
}