forked from cisco/ChezScheme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathht.ss
149 lines (144 loc) · 5.35 KB
/
ht.ss
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
#! ../bin/scheme --script
;;; ht.ss
;;; Copyright 1984-2016 Cisco Systems, Inc.
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.
#;(optimize-level 3)
(collect-request-handler void)
(module M (eqht symht gen-set eq-set sym-set gen-ref eq-ref
sym-ref print-htstats)
(define (eqht) (make-eq-hashtable))
(define (symht) (make-hashtable symbol-hash eq?))
(define refsym* (oblist))
(define setsym*
(fold-left
(lambda (ls x i) (if (fx< (modulo i 10) 1) ls (cons x ls)))
'()
refsym*
(enumerate refsym*)))
(define gen-set
(lambda (ht n)
(do ([n n (fx- n 1)])
((fx= n 0) ht)
(for-each
(lambda (x) (hashtable-set! ht x (list n)))
setsym*))))
(define eq-set
(lambda (ht n)
(do ([n n (fx- n 1)])
((fx= n 0) ht)
(for-each
(lambda (x) (eq-hashtable-set! ht x (list n)))
setsym*))))
(define sym-set
(lambda (ht n)
(do ([n n (fx- n 1)])
((fx= n 0) ht)
(for-each
(lambda (x) (symbol-hashtable-set! ht x (list n)))
setsym*))))
(define maybe-car (lambda (x) (and x (car x))))
(define gen-ref
(lambda (ht n)
(let f ([n n] [x #f])
(if (fx= n 0)
x
(do ([sym* refsym* (cdr sym*)]
[x x (maybe-car (hashtable-ref ht (car sym*) #f))])
((null? sym*) (f (fx- n 1) x)))))))
(define eq-ref
(lambda (ht n)
(let f ([n n] [x #f])
(if (fx= n 0)
x
(do ([sym* refsym* (cdr sym*)]
[x x (maybe-car (eq-hashtable-ref ht (car sym*) #f))])
((null? sym*) (f (fx- n 1) x)))))))
(define sym-ref
(lambda (ht n)
(let f ([n n] [x #f])
(if (fx= n 0)
x
(do ([sym* refsym* (cdr sym*)]
[x x (maybe-car (symbol-hashtable-ref ht (car sym*) #f))])
((null? sym*) (f (fx- n 1) x)))))))
(define print-htstats
(let ()
(include "hashtable-types.ss")
(lambda (ht)
(let ([ls** (map (if (eq-ht? ht)
(lambda (b)
(do ([b b (#%$tlc-next b)]
[ls '() (cons
(car (#%$tlc-keyval b))
ls)])
((fixnum? b) ls)))
(lambda (ls) (map car ls)))
(vector->list (ht-vec ht)))])
(let* ([n* (map length ls**)] [len (length n*)])
(printf "min = ~d, max = ~d, avg = ~,2f, med = ~d, stddev = ~,2f\n"
(apply min n*) (apply max n*) (/ (apply + n*) len)
(list-ref (sort < n*) (quotient len 2))
(let* ([mu (/ (apply + n*) len)])
(sqrt
(/ (apply + (map (lambda (n) (expt (- n mu) 2)) n*))
len))))
(printf
"a max-size bucket: ~s\n"
(let ([n (apply max n*)])
(cdr (find
(lambda (n.ls) (= (car n.ls) n))
(map cons n* ls**)))))))))))
(collect 0 1)
(let ()
(import M)
(define millis
(lambda (t)
(+ (* (time-second t) 1000)
(round (/ (time-nanosecond t) 1000000)))))
(define runs 10)
(define iterations 1000)
(define-syntax run
(syntax-rules ()
[(_ ?set ?ref ?make-ht)
(let ([set ?set] [ref ?ref] [make-ht ?make-ht])
(let loop ([runs runs] [st 0] [rt 0])
(if (fx= runs 0)
(begin
(printf "(time (~s ~s ~d) ~d)\n" '?set '?make-ht
iterations st)
(printf "(time (~s ~s ~d) ~d)\n" '?ref '?make-ht
iterations rt))
(let ([ht (make-ht)])
(let* ([st (begin
(collect 0 1)
(let ([t (current-time 'time-process)])
(set ht iterations)
(let ([t (time-difference
(current-time 'time-process)
t)])
(+ st (millis t)))))]
[rt (begin
(collect 0 1)
(let ([t (current-time 'time-process)])
(ref ht iterations)
(let ([t (time-difference
(current-time 'time-process)
t)])
(+ rt (millis t)))))])
(when (= runs 1) (print-htstats ht))
(loop (fx- runs 1) st rt))))))]))
(run gen-set gen-ref eqht)
(run gen-set gen-ref symht)
(run eq-set eq-ref eqht)
(run sym-set sym-ref symht))