Skip to content

Instantly share code, notes, and snippets.

@lockie
Last active February 3, 2023 12:45
Show Gist options
  • Save lockie/2521adbe761a8f789ef29b5c0ce2934c to your computer and use it in GitHub Desktop.
Save lockie/2521adbe761a8f789ef29b5c0ce2934c to your computer and use it in GitHub Desktop.
Common Lisp arrays benchmark; https://t.me/lisp_exile/13
(ql:quickload :trivial-benchmark)
(proclaim '(optimize (speed 3) (safety 0) (debug 0)
(compilation-speed 0) (space 0)))
(declaim (ftype (function (simple-vector) fixnum) sum-simple-vector))
(defun sum-simple-vector (v)
(loop
:with s :of-type fixnum := 0
:for x :of-type fixnum :across v
:do (incf s x)
:finally (return s)))
(declaim (ftype (function ((vector *)) fixnum) sum-vector))
(defun sum-vector (v)
(loop
:with s :of-type fixnum := 0
:for x :of-type fixnum :across v
:do (incf s x)
:finally (return s)))
(let ((simple (make-array 10000 :initial-element 42))
(adjustable (make-array 10000 :initial-element 42 :element-type 'fixnum
:adjustable t)))
(format t "~a ~a~%" (lisp-implementation-type) (lisp-implementation-version))
(asdf/run-program:run-program "uname -a" :output t)
(format t "~a ~a~%" (type-of simple) (type-of adjustable))
;; (princ 'sum-simple-vector)
;; (disassemble 'sum-simple-vector)
;; (princ 'sum-vector)
;; (disassemble 'sum-vector)
(princ "simple-vector")
(benchmark:with-timing (100000)
(sum-simple-vector simple))
(princ "adjustable vector")
(benchmark:with-timing (100000)
(sum-vector adjustable))
(princ "reallocating simple-vector")
(let ((i (the fixnum 10001))
(simple (make-array 10000 :initial-element 1)))
(benchmark:with-timing (100000)
(incf i (aref (adjust-array simple i) 1)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment