Files
aster/examples/script.ast
2026-02-06 11:46:27 +08:00

41 lines
642 B
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 压测:循环、递归、闭包
fn fib(n) {
if (n <= 1) {
return n
}
return fib(n - 1) + fib(n - 2)
}
fn make_adder(x) {
return fn(y) { return x + y }
}
let t0 = clock()
// 1. 循环压测100万次算术
let sum = 0
let i = 0
while (i < 1000000) {
sum = sum + i
i = i + 1
}
print("loop 1M:", sum)
// 2. 递归压测fib(25)
let f = fib(25)
print("fib(25):", f)
// 3. 闭包压测100万次调用
let add10 = make_adder(10)
let j = 0
let total = 0
while (j < 1000000) {
total = add10(j)
j = j + 1
}
print("closure 1M calls:", total)
let t1 = clock()
print("total time (s):", t1 - t0)