-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.numpad
53 lines (42 loc) · 1.67 KB
/
bench.numpad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
start = time_now()
// Helper functions for mathematical operations
fibonacci = n => if n <= 1 then n else fibonacci(n - 1) + fibonacci(n - 2)
factorial = n => if n <= 1 then 1 else n * factorial(n - 1)
is_divisible_by_any = (n, factors) =>
if len(factors) == 0 then false
else if n % factors[0] == 0 then true
else is_divisible_by_any(n, tail(factors))
is_prime = n =>
if n <= 1 then false
else if n <= 3 then true
else if n % 2 == 0 then false
else !is_divisible_by_any(n, collect(each(range(floor(sqrt(n)) - 2)) + 3))
// Complex number operations using records
complex_add = (a, b) => [a[0] + b[0], a[1] + b[1]]
complex_mul = (a, b) => [
a[0] * b[0] - a[1] * b[1],
a[0] * b[1] + a[1] * b[0]
]
complex_mag = z => z[0] * z[0] + z[1] * z[1]
mandelbrot = (x, y, max_iter, iter, z) =>
if iter >= max_iter then iter
else if complex_mag(z) > 4 then iter
else mandelbrot(
x, y, max_iter,
iter + 1,
complex_add(complex_mul(z, z), [x, y])
)
// Main computation that exercises different language features
benchmark = n => {
primes: filter(is_prime, range(n)),
fibs: collect(each(range(n)) with (i) => fibonacci(i)),
factorials: collect(each(range(n)) with (i) => factorial(i)),
reverse_test: reverse(range(n)),
mandel_test: collect(each [[-2, -1], [-1, 0], [0, 0], [0.25, 0]] with (point) => mandelbrot(point[0], point[1], 50, 0, [0, 0])),
sum_squares: sum(...collect(each(range(n)) with (i) => i * i)),
trig_test: collect(each(range(n)) with (i) => [sin(i), cos(i), tan(i)])
}
// Run benchmark
results = benchmark(20)
print("that took {} seconds", time_now() - start)
print("results: {}", results)