Skip to content

Instantly share code, notes, and snippets.

@takagi
Last active December 13, 2016 17:43
Show Gist options
  • Save takagi/7ec939d17ce202d7763583155b27f2a9 to your computer and use it in GitHub Desktop.
Save takagi/7ec939d17ce202d7763583155b27f2a9 to your computer and use it in GitHub Desktop.
(defun make-dvec (input-dimension initial-element)
(make-array input-dimension :element-type 'double-float :initial-element initial-element))
(defmacro dovec (vec var &body body)
`(loop for ,var fixnum from 0 to (1- (length ,vec)) do ,@body))
(defun dot (x y)
(declare (type (simple-array double-float) x y)
(optimize (speed 3) (safety 0)))
(let ((result 0.0d0))
(declare (type double-float result))
(dovec x i (incf result (* (aref x i) (aref y i))))
result))
(defun dot! (x y result)
(declare (type (simple-array double-float) x y)
(type (simple-array double-float 1) result)
(optimize (speed 3) (safety 0)))
(setf (aref result 0) 0d0)
(dovec x i (incf (aref result 0) (* (aref x i) (aref y i)))))
(defparameter dv1 (make-dvec 10000 2d0)) ; 100 -> 10000
(defparameter dv2 (make-dvec 10000 10d0)) ; 100 -> 10000
(defparameter result (make-dvec 1 0d0))
(time (loop repeat 100000 do (dot dv1 dv2))) ; 10,000,000 -> 100,000
;; Evaluation took:
;; 18.397 seconds of real time
;; 17.781488 seconds of total run time (17.004876 user, 0.776612 system)
;; [ Run times consist of 1.090 seconds GC time, and 16.692 seconds non-GC time. ]
;; 96.65% CPU
;; 41,492,780,804 processor cycles
;; 16,001,580,288 bytes consed
(time (loop repeat 100000 do (dot! dv1 dv2 result))) ; 10,000,000 -> 100,000
;; Evaluation took:
;; 4.168 seconds of real time
;; 4.125039 seconds of total run time (4.105985 user, 0.019054 system)
;; 98.97% CPU
;; 9,400,476,442 processor cycles
;; 34,304 bytes consed
(defun foo (x out)
(declare (optimize (speed 3) (safety 0)))
(declare (type double-float x))
(declare (type (simple-array double-float *) out))
(setf (aref out 0) (+ x x))
(values))
(disassemble #'foo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment