File: thash.scm

package info (click to toggle)
snd 26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,044 kB
  • sloc: ansic: 291,996; lisp: 260,569; ruby: 71,134; sh: 3,293; fortran: 2,342; csh: 1,067; cpp: 294; makefile: 294; python: 87; xml: 27; javascript: 1
file content (333 lines) | stat: -rw-r--r-- 9,826 bytes parent folder | download
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
(set! (*s7* 'hash-table-missing-key-value) #f)
(set! (*s7* 'heap-size) (* 12 1024000))

(define (reader)
  (let ((port (open-input-file "/home/bil/cl/bib"))
	(counts (make-hash-table))
	(start 0)
	(end 0)
	(new-pos 0))
    (do ((line (read-line port) (read-line port)))
	((eof-object? line))
      (set! new-pos 0)
      (do ((pos (char-position #\space line) (char-position #\space line (+ pos 1))))
	  ((not pos))
	(unless (= pos new-pos)
	  (set! start 
		(if (char-alphabetic? (string-ref line new-pos))
		    new-pos
		    (do ((k (+ new-pos 1) (+ k 1))) ; char-position here is slower!
			((or (char-alphabetic? (string-ref line k))
			     (>= k pos))
			 k))))
	  (set! end 
		(if (char-alphabetic? (string-ref line (- pos 1)))
		    pos
		    (do ((k (- pos 2) (- k 1)))
			((or (char-alphabetic? (string-ref line k))
			     (<= k start))
			 (+ k 1)))))
	  (when (> end start)
	    (let ((word (string->symbol (substring line start end))))
	      (hash-table-set! counts word (+ (or (hash-table-ref counts word) 0) 1)))))
	(set! new-pos (+ pos 1))))
    (close-input-port port)
    (sort! (copy counts (make-vector (hash-table-entries counts)))
	   (lambda (a b) (> (cdr a) (cdr b))))))

(format *stderr* "reader ")

(let ((counts (reader)))
  (unless (and (eq? (car (counts 0)) 'the)
	       (= (cdr (counts 0)) 62063))
    (do ((i 0 (+ i 1))) 
	((= i 40)) 
      (format *stderr* "~A: ~A~%" (car (counts i)) (cdr (counts i))))))

;; 1109: eval 254, resize_heap_to, fx_c_sss, fx_c_opssq_direct, fx_add_s1, make_symbol

;;; ----------------------------------------

(define global-val 0) ; for reader-cond in s7test.scm

(let ()
  (define (walk p counts)
    (if (pair? p)
	(begin
	  (walk (car p) counts)
	  (if (pair? (cdr p))
	      (walk (cdr p) counts)))
	(hash-table-set! counts p (+ (or (hash-table-ref counts p) 0) 1))))
  
  (define (s7test-reader)
    (let ((port (open-input-file "s7test.scm"))
	  (counts (make-hash-table)))
      (do ((expr (read port) (catch #t (lambda () (read port)) (lambda args 'error))))
	  ((eof-object? expr) 
	   (close-input-port port)
	   counts)
	(walk expr counts))))
  
  (define (sort-counts counts)
    (let ((len (hash-table-entries counts)))
      (do ((v (make-vector len))
	   (h (make-iterator counts))
	   (i 0 (+ i 1)))
	  ((= i len)
	   (sort! v (lambda (e1 e2) (> (cdr e1) (cdr e2))))
	   v)
	(vector-set! v i (iterate h)))))
  
  (sort-counts (s7test-reader)))

;; 1658: hash_equal_real 446, eval 257, hash_equal_complex 230, hash_equal_integer 175, hash_equal_ratio 144

;;; ----------------------------------------

(let ()
  (define (hash-ints calls)
    (let ((counts (make-hash-table)))
      (do ((i 0 (+ i 1))
	   (z (random 100) (random 100)))
	  ((= i calls) i)
	(hash-table-set! counts z (+ (or (hash-table-ref counts z) 0) 1)))))
  
  (let ((val (hash-ints 5000000)))
    (unless (= val 5000000)
      (format *stderr* "thash hash-ints: ~S?~%" val))))
;; 542: 184 fx_hash_table_increment_1, 135 fx_random_i, 80 hash_int, 51 resize_heap_to, 50 op_dox, 40 :opt_p_ppp_hash_table_increment

;;; ----------------------------------------

(define symbols (make-vector 1))
(define strings (make-vector 1))

(define (test1 size)
  (let ((int-hash (make-hash-table size))
	(p (cons #f #f)))
    (do ((i 0 (+ i 1))) 
	((= i size))
      (hash-table-set! int-hash i i))
    (do ((i 0 (+ i 1)))	
	((= i size))
      (unless (= (hash-table-ref int-hash i) i)
	(display "oops")))
    (for-each (lambda (key&value)
		(unless (= (car key&value) (cdr key&value))
		  (display "oops"))) ;(format *stderr* "hash iter ~A~%" key&value)))
	      (make-iterator int-hash p))
    (set! int-hash #f)))

(define (test2 size)
  (let ((int-hash (make-hash-table size =)))
    (do ((i 0 (+ i 1))) 
	((= i size)) 
      (hash-table-set! int-hash i i))
    (do ((i 0 (+ i 1)))	
	((= i size))
      (unless (= (hash-table-ref int-hash i) i)
	(display "oops")))
    (set! int-hash #f)))

(define (test3 size)
  (let ((flt-hash (make-hash-table size)))
    (do ((i 0 (+ i 1))) 
	((= i size)) 
      (hash-table-set! flt-hash (* i 2.0) i))
    (do ((i 0 (+ i 1)))	
	((= i size))
      (unless (= (hash-table-ref flt-hash (* 2.0 i)) i)
	(display "oops")))
    (set! flt-hash #f)))

(define (random-string)
  (let* ((len (+ 1 (random 8)))
	 (s (make-string len)))
    (do ((i 0 (+ i 1)))
	((= i len) s)
      (string-set! s i (integer->char (+ 20 (random 100)))))))

(define (test4 size)
  (let ((sym-hash (make-hash-table size)))
    (do ((i 0 (+ i 1))) 
	((= i size)) 
      (hash-table-set! sym-hash (vector-set! symbols i (string->symbol (vector-set! strings i (number->string i)))) i))
    (do ((i 0 (+ i 1))) 
	((= i size)) 
      (unless (= (hash-table-ref sym-hash (vector-ref symbols i)) i)
	(display "oops")))
    (set! sym-hash #f)))

(define (test5 size)
  (let ((str-hash (make-hash-table size eq?))) ; string=? here is pessimal because number->string above ruins hash_map_string
    (do ((i 0 (+ i 1))) 
	((= i size)) 
      (hash-table-set! str-hash (vector-ref strings i) i))
    (do ((i 0 (+ i 1))) 
	((= i size)) 
      (unless (= (hash-table-ref str-hash (vector-ref strings i)) i)
	(display "oops")))
    (set! str-hash #f)))

(define (test6 size)
  (let ((sym-hash (make-hash-table size eq?)))
    (do ((i 0 (+ i 1))) 
	((= i size)) 
      (hash-table-set! sym-hash (vector-ref symbols i) i))
    (do ((i 0 (+ i 1))) 
	((= i size)) 
      (unless (= (hash-table-ref sym-hash (vector-ref symbols i)) i)
	(display "oops")))
    (set! sym-hash #f)))

(define (test7 size)
  (let ((chr-hash (make-hash-table 256 char=?)))
    (do ((i 0 (+ i 1))) 
	((= i 256)) 
      (hash-table-set! chr-hash (integer->char i) i))
    (do ((i 0 (+ i 1))) 
	((= i 256)) 
      (unless (= (hash-table-ref chr-hash (integer->char i)) i)
	(display "oops")))
    (set! chr-hash #f)))

(define (test8 size)
  (let ((any-hash (make-hash-table size eq?)))
    (if (= size 1)
	(hash-table-set! any-hash (vector-set! strings 0 (list 0)) 0)
	(begin
	  (do ((i 0 (+ i 2)))
	      ((= i size))
	    (hash-table-set! any-hash (vector-set! strings i (list i)) i))
	  (do ((j 1 (+ j 2)))
	      ((>= j size))
	    (hash-table-set! any-hash (vector-set! strings j (int-vector j)) j))))
    (do ((i 0 (+ i 1))) 
	((= i size)) 
      (unless (= i (hash-table-ref any-hash (vector-ref strings i)))
	(display "oops")))
    (set! any-hash #f)))

(when (provided? 'pure-s7)
  (define (vector-fill! vect val . args)
    (if (vector? vect)
	(apply fill! vect val args)
	(error 'wrong-type-arg "vector-fill! argument should be a vector: ~A" str))))

(define (test9 size)
  (let ((any-hash1 (make-hash-table size eq?)))
    (if (= size 1)
	(hash-table-set! any-hash1 (vector-set! strings 0 (inlet 'a 0)) 0)
	(begin
	  (do ((i 0 (+ i 2)))
	      ((= i size))
	    (hash-table-set! any-hash1 (vector-set! strings i (inlet 'a i)) i))
	  (do ((j 1 (+ j 2))
	       (x 0.0 (+ x 2.0)))
	      ((>= j size))
	    (hash-table-set! any-hash1 (vector-set! strings j (float-vector x)) j))))
    (do ((i 0 (+ i 1))) 
	((= i size)) 
      (unless (= i (hash-table-ref any-hash1 (vector-ref strings i)))
	(display "oops")))
    (vector-fill! strings #f)
    (set! any-hash1 #f)))

(define (test10 size)
  (let ((cmp-hash (make-hash-table size =)))
    (do ((i 0 (+ i 1))) 
	((= i size)) 
      (hash-table-set! cmp-hash (complex i i) i))
    (do ((i 0 (+ i 1))) 
	((= i size)) 
      (unless (= (hash-table-ref cmp-hash (complex i i)) i)
	(display "oops")))
    (set! cmp-hash #f)))

(define (test11 size)
  (let ((int-hash (make-hash-table size equivalent?)))
    (do ((i 0 (+ i 1))) 
	((= i size)) 
      (hash-table-set! int-hash i i))
    (let-temporarily (((*s7* 'hash-table-float-epsilon) 1.0e-6))
      (do ((i 0 (+ i 1)))	
	  ((= i size))
	(unless (= (hash-table-ref int-hash (+ i (random 9.0e-7))) i)
	  (display "oops"))
	(unless (= (hash-table-ref int-hash (- i (random 9.0e-7))) i)
	  (display "oops"))
	(when (hash-table-ref int-hash (- i 1.0e-5 (random 1.0e-6)))
	  (display "oops")))
      (set! int-hash #f))))

(define (test12 size)
  (let ((rat-hash (make-hash-table size eqv?)))
    (do ((i 0 (+ i 1))) 
	((= i size))
      (hash-table-set! rat-hash (/ i 3) i))
    (do ((i 0 (+ i 1)))
	((= i size))
      (unless (= (hash-table-ref rat-hash (/ i 3)) i)
	(display "oops")))
    (set! rat-hash #f)))

(define (test13 size)
  (let ((vct-hash (make-hash-table size equal?)))
    (do ((i 0 (+ i 1))) 
	((= i size)) 
      (hash-table-set! vct-hash (float-vector i) i))
    (do ((i 0 (+ i 1)))	
	((= i size))
      (unless (= (hash-table-ref vct-hash (float-vector i)) i)
	(display "oops")))
    (set! vct-hash #f)))

(define (random-string-1)
  (let* ((len (+ 1 (random 20)))
	 (s (make-string len)))
    (do ((i 0 (+ i 1)))
	((= i len) s)
      (string-set! s i (integer->char (+ 20 (random 100)))))))

(define (test14 size)
  (when (< size 1000000) ; random-string is not interesting in this context
    (do ((i 0 (+ i 1))) 
	((= i size)) 
      (vector-set! strings i (random-string-1)))
    (let ((str-hash (make-hash-table size string=?)))
      (do ((i 0 (+ i 1))) 
	  ((= i size)) 
	(hash-table-set! str-hash (vector-ref strings i) i))
      (do ((i 0 (+ i 1))) 
	  ((= i size)) 
	(unless (hash-table-ref str-hash (vector-ref strings i))
	  (display "oops")))
      (set! str-hash #f))))

;; tmisc.scm has hash-table + typers

(define (test-hash size)
  (format *stderr* "~D " size)
  (set! symbols (make-vector size))
  (set! strings (make-vector size))
  (test1 size)
  (test2 size)
  (test3 size)
  (test4 size)
  (test5 size)
  (test6 size)
  (test7 size)
  (test8 size)
  (test9 size)
  (test10 size)
  (test11 size)
  (test12 size)
  (test13 size)
  (test14 size))

(for-each test-hash (list 1 10 100 1000 10000 100000 1000000))
(newline)

(when (> (*s7* 'profile) 0)
  (show-profile 200))
(exit)