/
timing-tests.rkt
274 lines (239 loc) · 7.98 KB
/
timing-tests.rkt
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#lang racket
(require "core.rkt")
(require "blame.rkt")
(require "combinators.rkt")
(require rackunit)
(provide (all-defined-out))
(define stdout (current-output-port))
(define (flush-stdout) (flush-output stdout))
;; This is to deal with async errors
(uncaught-exception-handler (λ (_) (displayln _) ((error-escape-handler))))
(define dblame (blame-labels 'server 'contract 'client))
(struct node (left value right) #:transparent)
(define treedc
(lambda (c1 s1 c2 s2 c3 s3 c4 s4)
(lambda (t b)
(match t
[(node l v r) (let ((nv (checkCon c3 s3 v b)))
(node
(checkCon (c2 nv) s2 l b)
nv
(checkCon (c4 nv) s4 r b)))]
[v (checkCon c1 s1 v b)]))))
(define bst-insert
(lambda (t ins)
(match t
[(node l v r) #:when (<= ins v) (node (bst-insert l ins) v r)]
[(node l v r) (node l v (bst-insert r ins))]
['() (node '() ins '())]
[v #:when (<= v ins) (node '() v (node '() ins '()))]
[v (node (node '() ins '()) v '())])))
(define bst-insert-d
(lambda (dt ins)
(match (force dt)
[(node l v r) #:when (<= ins (force v)) (node (bst-insert-d l ins) v r)]
[(node l v r) (node l v (bst-insert-d r ins))]
['() (node '() ins '())]
[v #:when (<= (force v) ins) (node '() v (node '() ins '()))]
[v (node (node '() ins '()) v '())])))
(define (bst-tree n)
(foldr
(lambda (x t) (bst-insert t x))
(foldr (lambda (x t) (bst-insert t x)) '() (range (quotient n 2)))
(drop (range n) (quotient n 2))))
(define bounded-bst-E
(lambda (lo hi)
(treedc
(predc null?) eager
(lambda (n) (bounded-bst-E lo n)) eager
(predc (lambda (x) (and (<= lo x) (<= x hi)))) eager
(lambda (n) (bounded-bst-E n hi)) eager)))
(define ((flip f) x y) (f y x))
(define full-bst (foldl (flip bst-insert) 5 '(3 7 2 4 6 8)))
(define gen-next (lambda (lo hi) (quotient (+ hi lo) 2)))
(define gen-bst-sequence
(lambda (hi)
(let loop ((lo 0) (hi hi))
(cond
[(> 2 (- hi lo)) '()]
[else (let ((next (gen-next lo hi)))
(append `(,next) (loop lo next) (loop next hi)))]))))
(define build-full-bst
(lambda (n)
(let ((elems (gen-bst-sequence (expt 2 n))))
(foldl (flip bst-insert) (node '() (car elems) '()) (cdr elems)))))
(define (full-check t)
(match t
[(node l v r) (let ((hl (full-check l))
(hr (full-check r)))
(if (and (number? hl) (number? hr) (= hr hl)) (add1 hl) #f))]
[_ 0]))
(define (full? t)
(number? (full-check t)))
(define full/a
(lambda (i)
(let ((il (make-channel))
(ir (make-channel)))
(treedc
(predc (lambda (n) (begin (channel-put i 0) #t))) eager
(lambda (_) (full/a il)) semi
(predc (lambda (n)
(let ((hr (sync (choice-evt ir il)))
(hl (sync (choice-evt ir il))))
(if (= hl hr)
(begin (channel-put i (add1 hl)) #t)
#f))))
async
(lambda (_) (full/a ir)) semi))))
(define full/a*
(lambda (i)
(let ((il (make-channel))
(ir (make-channel)))
(treedc
(predc (lambda (n) (begin (channel-put i 0) #t))) eager
(lambda (_) (full/a* il)) eager
(predc (lambda (n)
(let ((hr (sync (choice-evt ir il)))
(hl (sync (choice-evt ir il))))
(if (= hl hr)
(begin (channel-put i (add1 hl)) #t)
#f))))
async
(lambda (_) (full/a* ir)) eager))))
(define bst-contains?
(lambda (dt elem)
(match (force dt)
[(node l v r) #:when (= elem (force v)) #t]
[(node l v r) #:when (< elem (force v)) (bst-contains? l elem)]
[(node l v r) (bst-contains? r elem)]
['() #f])))
(define (print-dot)
(printf " .")
(flush-stdout))
(printf "Building trees for timing tests .")
(define full-bst-1 (build-full-bst 1))
(print-dot)
(define full-bst-2 (build-full-bst 2))
(print-dot)
(define full-bst-3 (build-full-bst 3))
(print-dot)
(define full-bst-4 (build-full-bst 4))
(print-dot)
(define full-bst-5 (build-full-bst 5))
(print-dot)
(define full-bst-6 (build-full-bst 6))
(print-dot)
(define full-bst-7 (build-full-bst 7))
(print-dot)
(define full-bst-8 (build-full-bst 8))
(print-dot)
(define full-bst-9 (build-full-bst 9))
(print-dot)
(define full-bst-10 (build-full-bst 10))
(print-dot)
(define full-bst-11 (build-full-bst 11))
(print-dot)
(define full-bst-12 (build-full-bst 12))
(print-dot)
(define full-bst-13 (build-full-bst 13))
(print-dot)
(define full-bst-14 (build-full-bst 14))
(print-dot)
(define full-bst-15 (build-full-bst 15))
(print-dot)
(define full-bst-16 (build-full-bst 16))
(print-dot)
(define full-bst-17 (build-full-bst 17))
(print-dot)
(define full-bst-18 (build-full-bst 18))
(print-dot)
(define full-bst-19 (build-full-bst 19))
(print-dot)
(define full-bst-20 (build-full-bst 20))
(print-dot)
(define full-bst-21 (build-full-bst 21))
(print-dot)
(define full-bst-22 (build-full-bst 22))
(print-dot)
(define full-bst-23 (build-full-bst 23))
(print-dot)
(define full-bst-24 (build-full-bst 24))
(print-dot)
(printf "~n")
(printf "Done building trees.~n")
(define ((ccon-full-test bst n))
(let* ((i (make-channel))
(tree (checkCon (full/a i) eager bst dblame)))
(bst-contains? tree n)))
(require racket/contract)
(define full/c (flat-named-contract 'full/c full?))
(define ((built-in-full-test bst n))
(let ((tree (contract full/c bst 'pos 'neg)))
(bst-contains? tree n)))
(define batch-time
(lambda (n thk)
(foldl
(lambda (x ls)
(let-values (((ans cpu real cpu*) (time-apply thk '())))
(cons `(,cpu ,real) ls)))
'()
(range n))))
(define compute-timing-avgs
(lambda (ls)
(let ((len (1.0 . * . (length ls))))
(map (lambda (x) (/ x len))
(foldl
(lambda (n ls)
`(,(+ (car n) (car ls)) ,(+ (cadr n) (cadr ls))))
'(0 0)
ls)))))
(define (tests iter)
(printf "Running timing tests .")
(let ((results
(foldl
(lambda (t n ls)
(printf " .")
(let ((num (random n)))
(append
ls
`((,(batch-time iter (ccon-full-test t n)) ,(batch-time iter (built-in-full-test t n)))))))
'()
`(
,full-bst-1
,full-bst-2
,full-bst-3
,full-bst-4
,full-bst-5
,full-bst-6
,full-bst-7
,full-bst-8
,full-bst-9
,full-bst-10
,full-bst-11
,full-bst-12
,full-bst-13
,full-bst-14
,full-bst-15
,full-bst-16
,full-bst-17
,full-bst-18
,full-bst-19
,full-bst-20
,full-bst-21
,full-bst-22
,full-bst-23
,full-bst-24
)
(cdr (range 25)))))
(printf "~n")
(printf "Computing timing test results.~n")
(map cons (range 1 (add1 (length results))) (map (lambda (x) (map compute-timing-avgs x)) results))))
(printf "Priming pump for timing tests.~n")
(batch-time 10 (ccon-full-test full-bst-10 10))
(batch-time 10 (built-in-full-test full-bst-10 10))
(batch-time 10 (ccon-full-test full-bst-10 10))
(batch-time 10 (ccon-full-test full-bst-10 10))
(batch-time 10 (built-in-full-test full-bst-10 10))
(batch-time 10 (built-in-full-test full-bst-10 10))
(printf "Starting main benchmark.~n")
(tests 100)