File: write-hex-benchmark.lsp

package info (click to toggle)
acl2 8.0dfsg-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 226,956 kB
  • sloc: lisp: 2,678,900; ansic: 6,101; perl: 5,816; xml: 3,586; cpp: 2,624; ruby: 2,576; makefile: 2,443; sh: 2,312; python: 778; yacc: 764; ml: 763; awk: 260; csh: 186; php: 171; lex: 165; tcl: 44; java: 41; asm: 23; haskell: 17
file content (149 lines) | stat: -rw-r--r-- 5,759 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
; 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)

;; write-hex performance testing

(defun test-builtin (ntimes nums stream)
  (declare (type fixnum ntimes))
  (format t "Testing FORMAT.~%")
  (loop for i fixnum from 1 to ntimes do
        (loop for num in nums do
              (format stream "~x" num))))

(defun test-safe (ntimes nums stream)
  (declare (type fixnum ntimes))
  (format t "Testing WRITE-HEX.~%")
  (loop for i fixnum from 1 to ntimes do
        (loop for num in nums do
              (write-hex num stream))))

(defun test-unsafe (ntimes nums stream)
  (declare (type fixnum ntimes))
  (format t "Testing SCARY-UNSAFE-WRITE-HEX.~%")
  (loop for i fixnum from 1 to ntimes do
        (loop for num in nums do
              (scary-unsafe-write-hex num stream))))

(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 x in '((32  . 1000000) ;; 2^N . NTIMES
                   (60  . 1000000)
                   (64  . 800000)
                   (80  . 600000)
                   (128 . 500000)
                   (256 . 300000)
                   (512 . 150000))
        collect
        (let* ((n      (car x))
               (ntimes (cdr x))
               (limit  (expt 2 n))
               (nums   (loop for i from 1 to 10 collect (random limit))))
          (format t "~%  --- Testing hex writing of numbers up to 2^~d --- ~%" n)
          (with-open-file (stream "/dev/null"
                                  :direction :output
                                  :if-exists :append)
            (let* ((unsafe-time  (progn (gc) (my-time (test-unsafe ntimes nums stream))))
                   (safe-time  (progn (gc) (my-time (test-safe ntimes nums stream))))
                   (fmt-time (progn (gc) (my-time (test-builtin ntimes nums stream)))))
              (list n fmt-time safe-time unsafe-time))))))



(progn
  (format t "~%")
  (format t "         N         FMT       SAFE/Speedup     UNSAFE/Speedup~%")
  (format t "---------------------------------------------------------------~%~%")
  (loop for elem in *times* do
        ;; Times
        (let* ((n        (first elem))
               (fmt      (car (second elem)))
               (safe     (car (third elem)))
               (unsafe   (car (fourth elem)))
               (sspeedup (if (< fmt safe)   (- (/ safe fmt))   (/ fmt safe)))
               (uspeedup (if (< fmt unsafe) (- (/ unsafe fmt)) (/ fmt unsafe))))
          (format t "~10D  ~10,2Fs ~10,2Fs/~3,2Fx ~10,2Fs/~3,2Fx~%"
                  n fmt 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 "~%"))