-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern-shootout.shen
248 lines (185 loc) · 6.35 KB
/
pattern-shootout.shen
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
(scm. "(optimize-level 3)")
(set *iterations* 1000000000)
(define repeat
N Thunk -> N where (<= N 0)
N Thunk -> (do (thaw Thunk)
(repeat (- N 1) Thunk)))
\* Benchmark 1: if/else with intermediate values let-bound *\
(define benchmark-1
Var1 Var2 Pass -> (if (cons? Var1)
(if (cons? Var2)
(let Var1/hd (hd Var1)
(if (= 2 Var1/hd)
(let Var1/tl (tl Var1)
Var2/hd (hd Var2)
(if (= 2 Var2/hd)
(let Var2/tl (tl Var2)
B Var1/tl
(if (= B Var2/tl)
halt
Pass))
Pass))
Pass))
Pass)
Pass))
\* Benchmark 2: if/else no intermediate values bound *\
(define benchmark-2
Var1 Var2 Pass -> (if (cons? Var1)
(if (cons? Var2)
(if (= 2 (hd Var1))
(if (= 2 (hd Var2))
(let B (tl Var1)
(if (= B (tl Var2))
halt
Pass))
Pass)
Pass)
Pass)
Pass))
\* Benchmark 3: if/else with and merging *\
(define benchmark-3
Var1 Var2 Pass -> (if (and (cons? Var1)
(cons? Var2)
(= 2 (hd Var1))
(= 2 (hd Var2))
(= (tl Var1)
(tl Var2)))
(let B (tl Var1)
halt)
Pass))
\* Benchmark 4: guard/freeze *\
(define guard
Fail true Ok -> (thaw Ok)
Fail false Ok -> (Fail false))
(define benchmark-4
Var1 Var2 Guard
->
(Guard (cons? Var1)
(freeze
(Guard (cons? Var2)
(freeze (let Var1/hd (hd Var1)
(Guard (= 2 Var1/hd)
(freeze (let Var1/tl (tl Var1)
(let Var2/hd (hd Var2)
(Guard (= 2 Var2/hd)
(freeze (let Var2/tl (tl Var2)
(let B Var1/tl
(Guard (= B Var2/tl)
(freeze halt))))))))))))))) )
\* Benchmark 5: CPS *\
(define k.hd
X L R -> (if (cons? X)
(R (hd X))
(L false)))
(define k.tl
X L R -> (if (cons? X)
(R (tl X))
(L false)))
(define k.guard
true Fail Ok -> (Ok true)
false Fail Ok -> (Fail false))
(define benchmark-5
Var1 Var2 Pass ->
(k.hd Var1 Pass (/. Var1/hd
(k.guard (= 2 Var1/hd) Pass (/. _
(k.tl Var1 Pass (/. Var1/tl
(k.hd Var2 Pass (/. Var2/hd
(k.guard (= 2 Var2/hd) Pass (/. _
(k.tl Var2 Pass (/. Var2/tl
(let B Var1/tl
(k.guard (= B Var2/tl) Pass (/. _
halt))))))))))))))))
\\ Benchmark 6: CPS local def
(scm.
"(define (benchmark-6 V93398 V93399 V93400)
(define
(k.guard V93386 V93387 V93388)
(if V93386
(V93388 V93386)
(V93387 V93386)))
(define
(k.tl V93357 V93358 V93359)
(if (pair? V93357)
(V93359 (cdr V93357))
(V93358 #f)))
(define
(k.hd V93351 V93352 V93353)
(if (pair? V93351)
(V93353 (car V93351))
(V93352 #f)))
(k.hd V93398 V93400
(lambda (Var1/hd)
(k.guard (= 2 Var1/hd) V93400
(lambda (_)
(k.tl V93398 V93400 (lambda (Var1/tl)
(k.hd V93399 V93400
(lambda (Var2/hd)
(k.guard (= 2 Var2/hd) V93400
(lambda (_)
(k.tl V93399 V93400
(lambda (Var2/tl)
((lambda (B)
(k.guard (= B Var2/tl) V93400
(lambda (_) (quote halt))))
Var1/tl))))))))))))))")
\\ Run
(output "Benchmark 1~%")
(let
Pass false
Var1 (cons 2 1)
Var2 (cons 2 1)
(scm.time
(repeat
(value *iterations*)
(freeze
(benchmark-1 Var1 Var2 Pass)))))
(output "Benchmark 2~%")
(let
Pass false
Var1 (cons 2 1)
Var2 (cons 2 1)
_ (scm.collect 4 4)
(scm.time
(repeat
(value *iterations*)
(freeze
(benchmark-2 Var1 Var2 Pass)))))
(output "Benchmark 3~%")
(let
Pass false
Var1 (cons 2 1)
Var2 (cons 2 1)
_ (scm.collect 4 4)
(scm.time
(repeat
(value *iterations*)
(freeze
(benchmark-3 Var1 Var2 Pass)))))
(output "Benchmark 4~%")
(let Guard (guard (lambda X X))
Var1 (cons 2 1)
Var2 (cons 2 1)
_ (scm.collect 4 4)
(scm.time
(repeat (value *iterations*)
(freeze (benchmark-4 Var1 Var2 Guard)))))
(output "Benchmark 5~%")
(let Var1 (cons 2 1)
Var2 (cons 2 1)
Pass (/. X X)
_ (scm.collect 4 4)
(scm.time
(repeat
(value *iterations*)
(freeze
(benchmark-5 Var1 Var2 Pass)))))
(output "Benchmark 6~%")
(let Var1 (cons 2 1)
Var2 (cons 2 1)
Pass (/. X X)
_ (scm.collect 4 4)
(scm.time
(repeat
(value *iterations*)
(freeze
(scm.benchmark-6 Var1 Var2 Pass)))))