-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.py
91 lines (77 loc) · 1.78 KB
/
bench.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from repl import ecio_eval
from stats import display_stats
def run_benchmark():
recursive_fib = '''
(begin
(def fibonacci
(λ (n)
(if (< n 2)
n
(_+ (fibonacci (_- n 1))
(fibonacci (_- n 2))))))
(fibonacci {}))
'''
iterative_fib = '''
(begin
(def fibonacci
(λ (n)
(def loop
(λ (a b count)
(if (= count n)
a
(loop b (_+ a b) (_+ count 1)))))
(loop 0 1 0)))
(fibonacci {}))
'''
mutative_fib = '''
(begin
(def fibonacci
(λ (n)
(def a 0)
(def b 1)
(def count 0)
(def loop
(λ ()
(if (= count n)
a
(begin
(def temp b)
(set! b (_+ a b))
(set! a temp)
(set! count (_+ count 1))
(loop)))))
(loop)))
(fibonacci {}))
'''
fib_vals = {
0: 0,
1: 1,
2: 1,
3: 2,
4: 3,
5: 5,
6: 8,
7: 13,
8: 21,
9: 34,
10: 55,
}
functions = {
'RECURSIVE': recursive_fib,
'ITERATIVE': iterative_fib,
'MUTATIVE': mutative_fib,
}
for function_name, function in functions.items():
print(function_name)
for i in range(11):
print('n = {}'.format(i))
print()
expected = fib_vals[i]
result = ecio_eval(function.format(i))
if result != expected:
print('Incorrect result')
return
display_stats(1)
print()
if __name__ == '__main__':
run_benchmark()