// Optimized variant 2: extends opt1 with faster Leibniz + prime checks.
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
bind t0 = time_now()
bind r = fib_iter(30)
print("BENCH fib RESULT ", r, " TIME ", time_now() - t0)
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)
// Pair two terms per loop: 1/(4k+1) - 1/(4k+3)
bind t0 = time_now()
bind acc = 0.0
bind k = 0
while k < 2500000 {
bind d = 4.0 * k
bind acc = acc + 1.0 / (d + 1.0) - 1.0 / (d + 3.0)
bind k = k + 1
}
bind piapprox = 4.0 * acc
print("BENCH leibniz RESULT ", piapprox, " TIME ", time_now() - t0)
// 6k+-1 primality test
bind t0 = time_now()
bind count = 0
bind n = 2
while n < 50000 {
bind is_p = 1
if n > 3 {
if n % 2 == 0 {
bind is_p = 0
} else {
if n % 3 == 0 {
bind is_p = 0
} else {
bind d = 5
bind step = 2
while d * d <= n {
if n % d == 0 {
bind is_p = 0
bind d = n
}
bind d = d + step
bind step = 6 - step
}
}
}
}
bind count = count + is_p
bind n = n + 1
}
print("BENCH primes RESULT ", count, " TIME ", time_now() - t0)
bind t0 = time_now()
bind W = 200
bind H = 200
bind maxiter = 100
bind total = 0
bind py = 0
while py < H {
bind px = 0
while px < W {
bind x0 = (px / W) * 3.5 - 2.5
bind y0 = (py / H) * 2.0 - 1.0
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 px = px + 1
}
bind py = py + 1
}
print("BENCH mandelbrot RESULT ", total, " TIME ", time_now() - t0)
bind t0 = time_now()
bind N2 = 1000000
bind sr = 44100.0
bind sum = 0.0
bind j = 0
while j < N2 {
bind t = j / sr
bind sample = sin(2.0 * PI * 220.0 * t + sin(2.0 * PI * 440.0 * t))
bind sum = sum + sample
bind j = j + 1
}
print("BENCH fm_synth RESULT ", sum, " TIME ", time_now() - t0)
}