1 (defpackage :sb-gmp-bench
(:use
"COMMON-LISP"))
3 (in-package :sb-gmp-bench
)
5 (defparameter *stream
* t
)
7 (defparameter *state
* nil
)
9 (defun bench-+ () ; limbs: never
10 (macrolet ((tstfun (f a b
)
15 (loop for limbs fixnum from
2
17 until
(or (= gmp-win
5)
18 (= limbs
78)) ; > 78 exhausts default heap size
22 collect
(sb-gmp::bassert
(sb-gmp:random-bitcount
*state
* (* limbs sb-vm
:n-word-bits
)))
24 collect
(sb-gmp::bassert
(sb-gmp:random-bitcount
*state
* (* limbs sb-vm
:n-word-bits
)))
28 (let ((r-sbcl (progn (sb-ext:gc
)
29 (sb-ext:call-with-timing
32 (tstfun + list-a list-b
))))
33 (r-gmp (progn (sb-ext:gc
)
34 (sb-ext:call-with-timing
37 (tstfun sb-gmp
:mpz-add list-a list-b
)))))
38 (format *stream
* "limbs: ~s~%Time SBCL: ~s~%Time GMP: ~s~%"
40 (when (< (getf time2
:PROCESSOR-CYCLES
)
41 (getf time1
:PROCESSOR-CYCLES
))
43 (if (= (length r-sbcl
) (length r-gmp
))
44 (format *stream
* "Test PASSED~2%")
45 (format *stream
* "Test FAILED~2%"))))))))
48 (defun bench-* () ; limbs 6
49 (macrolet ((tstfun (f a b
)
54 (loop for limbs fixnum from
2
56 until
(or (= gmp-win
3)
61 collect
(sb-gmp::bassert
(sb-gmp:random-bitcount
*state
* (* limbs sb-vm
:n-word-bits
)))
63 collect
(sb-gmp::bassert
(sb-gmp:random-bitcount
*state
* (* limbs sb-vm
:n-word-bits
)))
67 (let ((r-sbcl (progn (sb-ext:gc
)
68 (sb-ext:call-with-timing
71 (tstfun * list-a list-b
))))
72 (r-gmp (progn (sb-ext:gc
)
73 (sb-ext:call-with-timing
76 (tstfun sb-gmp
:mpz-mul list-a list-b
)))))
77 (format *stream
* "limbs: ~s~%Time SBCL: ~s~%Time GMP: ~s~%"
79 (when (< (getf time2
:PROCESSOR-CYCLES
)
80 (getf time1
:PROCESSOR-CYCLES
))
82 (if (= (length r-sbcl
) (length r-gmp
))
83 (format *stream
* "Test PASSED~2%")
84 (format *stream
* "Test FAILED~2%"))))))))
86 (defun bench-/ () ; limbs 3 / 2
87 (macrolet ((tstfun (f a b
)
92 (loop for limbs fixnum from
2
93 for limbs_b fixnum from
1
95 until
(or (= gmp-win
3)
100 collect
(sb-gmp::bassert
(sb-gmp:random-bitcount
*state
* (* limbs sb-vm
:n-word-bits
)))
102 collect
(sb-gmp::bassert
(sb-gmp:random-bitcount
*state
* (* limbs_b sb-vm
:n-word-bits
)))
106 (format t
"bench it~%")
107 (let ((r-sbcl (progn (sb-ext:gc
)
108 (sb-ext:call-with-timing
109 (lambda (&rest plist
)
111 (tstfun truncate list-a list-b
))))
112 (r-gmp (progn (sb-ext:gc
)
113 (sb-ext:call-with-timing
114 (lambda (&rest plist
)
116 (tstfun sb-gmp
:mpz-tdiv list-a list-b
)))))
117 (format *stream
* "limbs: ~s~%Time SBCL: ~s~%Time GMP: ~s~%"
119 (when (< (getf time2
:PROCESSOR-CYCLES
)
120 (getf time1
:PROCESSOR-CYCLES
))
122 (if (= (length r-sbcl
) (length r-gmp
))
123 (format *stream
* "Test PASSED~2%")
124 (format *stream
* "Test FAILED~2%"))))))))
126 (defun bench-gcd () ; limbs: always
127 (macrolet ((tstfun (f a b
)
132 (loop for limbs fixnum from
2
133 for limbs_b fixnum from
1
135 until
(or (= gmp-win
3)
140 collect
(sb-gmp::bassert
(sb-gmp:random-bitcount
*state
* (* limbs sb-vm
:n-word-bits
)))
142 collect
(sb-gmp::bassert
(sb-gmp:random-bitcount
*state
* (* limbs_b sb-vm
:n-word-bits
)))
146 (format t
"bench it~%")
147 (let ((r-sbcl (progn (sb-ext:gc
)
148 (sb-ext:call-with-timing
149 (lambda (&rest plist
)
151 (tstfun gcd list-a list-b
))))
152 (r-gmp (progn (sb-ext:gc
)
153 (sb-ext:call-with-timing
154 (lambda (&rest plist
)
156 (tstfun sb-gmp
:mpz-gcd list-a list-b
)))))
157 (format *stream
* "limbs: ~s~%Time SBCL: ~s~%Time GMP: ~s~%"
159 (when (< (getf time2
:PROCESSOR-CYCLES
)
160 (getf time1
:PROCESSOR-CYCLES
))
162 (if (= (length r-sbcl
) (length r-gmp
))
163 (format *stream
* "Test PASSED~2%")
164 (format *stream
* "Test FAILED~2%"))))))))
166 (defun bench-isqrt () ; limbs: always
167 (macrolet ((tstfun (f a
)
171 (loop for limbs fixnum from
1
173 until
(or (= gmp-win
3)
178 collect
(sb-gmp::bassert
(sb-gmp:random-bitcount
*state
* (* limbs sb-vm
:n-word-bits
)))
182 (format t
"bench it~%")
183 (let ((r-sbcl (progn (sb-ext:gc
)
184 (sb-ext:call-with-timing
185 (lambda (&rest plist
)
187 (tstfun isqrt list-a
))))
188 (r-gmp (progn (sb-ext:gc
)
189 (sb-ext:call-with-timing
190 (lambda (&rest plist
)
192 (tstfun sb-gmp
:mpz-sqrt list-a
)))))
193 (format *stream
* "limbs: ~s~%Time SBCL: ~s~%Time GMP: ~s~%"
195 (when (< (getf time2
:PROCESSOR-CYCLES
)
196 (getf time1
:PROCESSOR-CYCLES
))
198 (if (= (length r-sbcl
) (length r-gmp
))
199 (format *stream
* "Test PASSED~2%")
200 (format *stream
* "Test FAILED~2%"))))))))
202 (defun bench-q+ () ; limbs: always
203 (macrolet ((tstfun (f a b
)
208 (loop for limbsa fixnum from
2
209 for limbsb fixnum from
1
211 until
(or (= gmp-win
5)
216 collect
(/ (sb-gmp:random-bitcount
*state
* (* limbsa sb-vm
:n-word-bits
))
217 (sb-gmp:random-bitcount
*state
* (* limbsb sb-vm
:n-word-bits
)))
219 collect
(/ (sb-gmp:random-bitcount
*state
* (* limbsa sb-vm
:n-word-bits
))
220 (sb-gmp:random-bitcount
*state
* (* limbsb sb-vm
:n-word-bits
)))
224 (let ((r-sbcl (progn (sb-ext:gc
)
225 (sb-ext:call-with-timing
226 (lambda (&rest plist
)
228 (tstfun + list-a list-b
))))
229 (r-gmp (progn (sb-ext:gc
)
230 (sb-ext:call-with-timing
231 (lambda (&rest plist
)
233 (tstfun sb-gmp
:mpq-add list-a list-b
)))))
234 (format *stream
* "limbs: ~s~%Time SBCL: ~s~%Time GMP: ~s~%"
236 (when (< (getf time2
:PROCESSOR-CYCLES
)
237 (getf time1
:PROCESSOR-CYCLES
))
239 (if (= (length r-sbcl
) (length r-gmp
))
240 (format *stream
* "Test PASSED~2%")
241 (format *stream
* "Test FAILED~2%"))))))))
243 (defun bench-q* () ; limbs: always
244 (macrolet ((tstfun (f a b
)
249 (loop for limbsa fixnum from
2
250 for limbsb fixnum from
1
252 until
(or (= gmp-win
5)
257 collect
(/ (sb-gmp:random-bitcount
*state
* (* limbsa sb-vm
:n-word-bits
))
258 (sb-gmp:random-bitcount
*state
* (* limbsb sb-vm
:n-word-bits
)))
260 collect
(/ (sb-gmp:random-bitcount
*state
* (* limbsa sb-vm
:n-word-bits
))
261 (sb-gmp:random-bitcount
*state
* (* limbsb sb-vm
:n-word-bits
)))
265 (let ((r-sbcl (progn (sb-ext:gc
)
266 (sb-ext:call-with-timing
267 (lambda (&rest plist
)
269 (tstfun * list-a list-b
))))
270 (r-gmp (progn (sb-ext:gc
)
271 (sb-ext:call-with-timing
272 (lambda (&rest plist
)
274 (tstfun sb-gmp
:mpq-mul list-a list-b
)))))
275 (format *stream
* "limbs: ~s~%Time SBCL: ~s~%Time GMP: ~s~%"
277 (when (< (getf time2
:PROCESSOR-CYCLES
)
278 (getf time1
:PROCESSOR-CYCLES
))
280 (if (= (length r-sbcl
) (length r-gmp
))
281 (format *stream
* "Test PASSED~2%")
282 (format *stream
* "Test FAILED~2%"))))))))
285 (let ((*state
* (sb-gmp:make-gmp-rstate
)))
286 (sb-gmp:rand-seed
*state
* 1234)