// Optimized variant 3: includes sieve for primes.
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)
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)
// Sieve of Eratosthenes for n < 50000
bind t0 = time_now()
bind LIMIT = 50000
bind is_comp = list_new()
bind idx = 0
while idx < LIMIT {
list_push(is_comp, 0)
bind idx = idx + 1
}
bind p = 2
while p * p < LIMIT {
if list_get(is_comp, p) == 0 {
bind m = p * p
while m < LIMIT {
list_set(is_comp, m, 1)
bind m = m + p
}
}
bind p = p + 1
}
bind count = 0
bind n = 2
while n < LIMIT {
if list_get(is_comp, n) == 0 {
bind count = count + 1
}
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 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)
bind t0 = time_now()
bind N2 = 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 < N2 {
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)
}