File: read-hex-benchmark.lsp

package info (click to toggle)
acl2 8.5dfsg-5
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 991,452 kB
  • sloc: lisp: 15,567,759; javascript: 22,820; cpp: 13,929; ansic: 12,092; perl: 7,150; java: 4,405; xml: 3,884; makefile: 3,507; sh: 3,187; ruby: 2,633; ml: 763; python: 746; yacc: 723; awk: 295; csh: 186; php: 171; lex: 154; tcl: 49; asm: 23; haskell: 17
file content (316 lines) | stat: -rw-r--r-- 12,444 bytes parent folder | download | duplicates (6)
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
; Fastnumio - Efficient hex string I/O ops for Common Lisp streams
; Copyright (C) 2015 Centaur Technology
;
; Contact:
;   Centaur Technology Formal Verification Group
;   7600-C N. Capital of Texas Highway, Suite 300, Austin, TX 78731, USA.
;   http://www.centtech.com/
;
; License: (An MIT/X11-style license)
;
;   Permission is hereby granted, free of charge, to any person obtaining a
;   copy of this software and associated documentation files (the "Software"),
;   to deal in the Software without restriction, including without limitation
;   the rights to use, copy, modify, merge, publish, distribute, sublicense,
;   and/or sell copies of the Software, and to permit persons to whom the
;   Software is furnished to do so, subject to the following conditions:
;
;   The above copyright notice and this permission notice shall be included in
;   all copies or substantial portions of the Software.
;
;   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;   DEALINGS IN THE SOFTWARE.
;
; Original author: Jared Davis <jared@centtech.com>

(ql:quickload :fastnumio)
(ql:quickload :trivial-garbage)
(in-package "FASTNUMIO")

(defun get-bytes ()
  #+ccl
  (ccl::total-bytes-allocated)
  #+sbcl
  (sb-ext::get-bytes-consed)
  #+(and (not sbcl) (not ccl))
  0)

(format t "Writing test files for reading.~%")

; We write out different files (but with the same data) because Lisp's READ
; function won't know to read it as a hex number unless it's got a leading
; #x..., but we don't want/need that for our own read-hex function.

(with-open-file (plain "/dev/shm/u32s.txt" :direction :output :if-exists :supersede)
  (with-open-file (sharp "/dev/shm/sharp-u32s.txt" :direction :output :if-exists :supersede)
    (loop for i fixnum from 1 to 1000000 do
          (let ((num (random (expt 2 32))))
            (format plain "~x~%" num)
            (format sharp "#x~x~%" num)))))

(with-open-file (plain "/dev/shm/u64s.txt" :direction :output :if-exists :supersede)
  (with-open-file (sharp "/dev/shm/sharp-u64s.txt" :direction :output :if-exists :supersede)
    (loop for i fixnum from 1 to 1000000 do
          (let ((num (random (expt 2 64))))
            (format plain "~x~%" num)
            (format sharp "#x~x~%" num)))))

(with-open-file (plain "/dev/shm/u128s.txt" :direction :output :if-exists :supersede)
  (with-open-file (sharp "/dev/shm/sharp-u128s.txt" :direction :output :if-exists :supersede)
    (loop for i fixnum from 1 to 500000 do
          (let ((num (random (expt 2 128))))
            (format plain "~x~%" num)
            (format sharp "#x~x~%" num)))))

(with-open-file (plain "/dev/shm/u512s.txt" :direction :output :if-exists :supersede)
  (with-open-file (sharp "/dev/shm/sharp-u512s.txt" :direction :output :if-exists :supersede)
    (loop for i fixnum from 1 to 200000 do
          (let ((num (random (expt 2 512))))
            (format plain "~x~%" num)
            (format sharp "#x~x~%" num)))))

(defun test-builtin (ntimes sharp-filename)
  (format t "Testing READ.~%")
  (loop for i fixnum from 1 to ntimes
        do
        (with-open-file (stream sharp-filename :direction :input)
          (let ((elem nil))
            (loop do
                  (let ((tmp (read stream nil nil)))
                    (if tmp
                        (setq elem tmp)
                      (loop-finish))))
            elem))))

(defun eat-whitespace (stream)
  (let ((char (read-char stream nil)))
    (cond ((not char)
           stream)
          ((or (eql char #\Space)
               (eql char #\Newline))
           stream)
          (t
           (unread-char char stream)
           stream))))

(defun test-safe (ntimes plain-filename)
  (format t "Testing READ-HEX.~%")
  (loop for i fixnum from 1 to ntimes
        do
        (with-open-file (stream plain-filename :direction :input)
          (let ((elem nil))
            (loop do
                  (eat-whitespace stream)
                  (let ((tmp (read-hex stream)))
                    (if tmp
                        (setq elem tmp)
                      (loop-finish))))
            elem))))

(defun test-unsafe (ntimes plain-filename)
  (format t "Testing SCARY-UNSAFE-READ-HEX.~%")
  (loop for i fixnum from 1 to ntimes
        do
        (with-open-file (stream plain-filename :direction :input)
          (let ((elem nil))
            (loop do
                  (eat-whitespace stream)
                  (let ((tmp (scary-unsafe-read-hex stream)))
                    (if tmp
                        (setq elem tmp)
                      (loop-finish))))
            elem))))

(defun gc ()
  (tg::gc :full t :verbose nil))

(defmacro my-time (form)
  ;; Returns (cons seconds bytes)
  `(let ((start-bytes (get-bytes))
         (start-time  (get-internal-real-time))
         (blah        (time ,form))
         (end-time    (get-internal-real-time))
         (end-bytes   (get-bytes)))
     (declare (ignore blah))
     (cons (/ (coerce (- end-time start-time) 'float)
              internal-time-units-per-second)
           (- end-bytes start-bytes))))

(defun nice-bytes (x)
  (cond ((< x (expt 2 10))
         (format nil "~5DB" x))
        ((< x (expt 2 20))
         (format nil "~5,1FK" (/ (coerce x 'float) (expt 2 10))))
        ((< x (expt 2 30))
         (format nil "~5,1FM" (/ (coerce x 'float) (expt 2 20))))
        (t
         (format nil "~5,1FG" (/ (coerce x 'float) (expt 2 30))))))


(defparameter *times*
  (loop for test in '((32  "/dev/shm/sharp-u32s.txt"  "/dev/shm/u32s.txt")
                      (64  "/dev/shm/sharp-u64s.txt"  "/dev/shm/u64s.txt")
                      (128 "/dev/shm/sharp-u128s.txt" "/dev/shm/u128s.txt")
                      (512 "/dev/shm/sharp-u512s.txt" "/dev/shm/u512s.txt")
                      )
        collect
        (let ((n          (first test))
              (sharp-file (second test))
              (plain-file (third test))
              (ntimes     5))
          (format t "~% --- Testing reads of random numbers under 2^~d ---~%" n)
          (let* ((builtin-time   (progn (gc) (my-time (test-builtin ntimes sharp-file))))
                 (safe-time      (progn (gc) (my-time (test-safe ntimes plain-file))))
                 (unsafe-time    (progn (gc) (my-time (test-unsafe ntimes plain-file)))))
            (list n builtin-time safe-time unsafe-time)))))

(progn
  (format t "~%")
  (format t "         N        READ       SAFE/Speedup     UNSAFE/Speedup~%")
  (format t "--------------------------------------------------------------~%~%")
  (loop for elem in *times* do
        ;; Times
        (let* ((n        (first elem))
               (builtin  (car (second elem)))
               (safe     (car (third elem)))
               (unsafe   (car (fourth elem)))
               (sspeedup (if (< builtin safe)   (- (/ safe builtin))   (/ builtin safe)))
               (uspeedup (if (< builtin unsafe) (- (/ unsafe builtin)) (/ builtin unsafe))))
          (format t "~10D  ~10,2Fs ~10,2Fs/~3,2Fx ~10,2Fs/~3,2Fx~%"
                  n builtin safe sspeedup unsafe uspeedup
                  ))
        ;; Bytes
        (let* ((builtin  (cdr (second elem)))
               (safe     (cdr (third elem)))
               (unsafe   (cdr (fourth elem)))
               (sspeedup (if (eql builtin 0)
                             "???"
                           (* 100 (/ (coerce safe 'float) builtin))))
               (uspeedup (if (eql builtin 0)
                             "???"
                           (* 100 (/ (coerce unsafe 'float) builtin)))))
          (format t "~10a       ~7a    ~7a ~3,1F%      ~7a ~3,1F%~%"
                  ""
                  (nice-bytes builtin)
                  (nice-bytes safe) sspeedup
                  (nice-bytes unsafe) uspeedup))
        (format t "~%"))
  (format t "--------------------------------------------------------------~%")
  (format t "~%"))

(progn
  (format t "Deleting test files.~%")
  (delete-file "/dev/shm/sharp-u32s.txt")
  (delete-file "/dev/shm/sharp-u64s.txt")
  (delete-file "/dev/shm/sharp-u128s.txt")
  (delete-file "/dev/shm/sharp-u512s.txt")
  (delete-file "/dev/shm/u32s.txt")
  (delete-file "/dev/shm/u64s.txt")
  (delete-file "/dev/shm/u128s.txt")
  (delete-file "/dev/shm/u512s.txt")
  )



;; (time (read-sharp-file ))   ;; 1.98 seconds, 0 bytes
;; (time (read-sharp-file "/dev/shm/sharp-u64s.txt"))   ;; 2.866 seconds, 53 MB
;; (time (read-sharp-file "/dev/shm/sharp-u128s.txt"))  ;; 6.353 seconds, 1 GB


;; (time (my-read-file ))   ;; .430 seconds, 144 MB allocated (why??)
;; (time (my-read-file "/dev/shm/u64s.txt"))   ;; .718 seconds, 190 MB allocated
;; (time (my-read-file "/dev/shm/u128s.txt"))  ;; 1.627 seconds, 271 MB allocated




;; (defun read-file-lines (filename)
;;   (let ((len 0))
;;     (with-open-file (stream filename :direction :input)
;;       (loop do
;;             (let ((line (read-line stream nil)))
;;               (when (not line)
;;                 (loop-finish))
;;               ;; Otherwise SBCL optimizes things away
;;               (incf len (length line)))))))

;; (time (read-file-lines "/dev/shm/sharp-u32s.txt"))   ;; 1.95 seconds, 0 bytes
;; (time (read-file-lines "/dev/shm/sharp-u64s.txt"))   ;; 2.03 seconds, 80 MB
;; (time (read-file-lines "/dev/shm/sharp-u128s.txt"))  ;; 2.15 seconds, 144 MB

;; ; So this is a bit depressing, read-line is pretty damn expensive

;; (defun read-file-chars (filename)
;;   (with-open-file (stream filename :direction :input)
;;     (loop do
;;           (let ((c (read-char stream nil)))
;;             (when (not c)
;;               (loop-finish))))))

;; (time (read-file-chars "/dev/shm/sharp-u32s.txt"))  ;; 0.188 seconds, 0 bytes
;; (time (read-file-chars "/dev/shm/sharp-u64s.txt"))  ;; 0.314 seconds, 0 bytes
;; (time (read-file-chars "/dev/shm/sharp-u128s.txt")) ;; 0.574 seconds, 0 bytes

;; ; So that is more promising.

;; ; There is also read-sequence, which may be faster.


;; (defun read-file-seq (filename)
;;   (let ((buf (make-array 80 :element-type 'character)))
;;     (with-open-file (stream filename :direction :input)
;;       (loop do
;;             (let ((c (read-sequence buf stream)))
;;               (when (< c 80)
;;                 (loop-finish)))))))

;; (time (read-file-seq "/dev/shm/sharp-u32s.txt"))  ;; 0.133 seconds, 0 bytes
;; (time (read-file-seq "/dev/shm/sharp-u64s.txt"))  ;; 0.221 seconds, 0 bytes
;; (time (read-file-seq "/dev/shm/sharp-u128s.txt")) ;; 0.396 seconds, 0 bytes

;; ; So that's 1.4x faster, but it's not very easy to write a nice high-level
;; ; interface to it, because we don't know where the EOLs are.


;; ; But we'll either need to unread characters or use peek-char...

;; (defun read-file-peek-chars (filename)
;;   (with-open-file (stream filename :direction :input)
;;     (loop do
;;           (let ((c (peek-char nil stream nil)))
;;             (when (not c)
;;               (loop-finish))
;;             (setq c (read-char stream nil))))))

;; (time (read-file-peek-chars "/dev/shm/sharp-u32s.txt"))  ;; 1.06 seconds
;; (time (read-file-peek-chars "/dev/shm/sharp-u64s.txt"))  ;; 1.78 seconds
;; (time (read-file-peek-chars "/dev/shm/sharp-u128s.txt")) ;; 3.27 seconds

;; ; So that's horrible.  How can that take so fucking long?

;; (defun read-file-peek-chars2 (filename)
;;   (with-open-file (stream filename :direction :input)
;;     (loop do
;;           (let ((c (read-char stream nil)))
;;             (when (not c)
;;               (loop-finish))
;;             (when (or (eql c #\Newline)
;;                       (eql c #\Space))
;;               (unread-char c stream)
;;               (read-char stream nil))))))

;; (time (read-file-peek-chars2 "/dev/shm/sharp-u32s.txt")) ;; 0.228 seconds
;; (time (read-file-peek-chars2 "/dev/shm/sharp-u64s.txt")) ;; 0.363 seconds
;; (time (read-file-peek-chars2 "/dev/shm/sharp-u128s.txt")) ;; 0.623 seconds

;; ; That's much better.  Ok, so I guess we need to base our parser on read-char
;; ; and just use unread-char when we encounter a non-hex digit.

;; ; The general idea