Skip to content

Instantly share code, notes, and snippets.

@inconvergent
Created March 26, 2019 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inconvergent/9fddd7710c8195028d5be7d92c41d715 to your computer and use it in GitHub Desktop.
Save inconvergent/9fddd7710c8195028d5be7d92c41d715 to your computer and use it in GitHub Desktop.
(defstruct vec2
(x nil :type double-float :read-only t)
(y nil :type double-float :read-only t))
(defstruct (vec3 (:include vec2))
(z nil :type double-float :read-only t))
(declaim (inline make-vec2))
(declaim (inline make-vec3))
(declaim (inline add2))
(defun add2 (v w)
(declare (optimize (safety 0) (speed 3) (debug 0))
(vec2 v w))
(make-vec2 :x (+ (vec2-x v) (vec2-x w))
:y (+ (vec2-y v) (vec2-y w))))
(declaim (inline add3))
(defun add3 (v w)
(declare (optimize (safety 0) (speed 3) (debug 0))
(vec3 v w))
(make-vec3 :x (+ (vec3-x v) (vec3-x w))
:y (+ (vec3-y v) (vec3-y w))
:z (+ (vec3-z v) (vec3-z w))))
(declaim (inline add))
(defgeneric add (v w)
(:method ( (v vec2) (w vec2) )
(add2 v w))
(:method ((v vec3) (w vec3))
(add3 v w)))
(declaim (inline rnd2))
(defun rnd2 ()
(declare (optimize (safety 0) (speed 3) (debug 0)))
(make-vec2 :x (random 1d0) :y (random 1d0)))
(declaim (inline rnd3))
(defun rnd3 ()
(declare (optimize (safety 0) (speed 3) (debug 0)))
(make-vec3 :x (random 1d0) :y (random 1d0) :z (random 1d0)))
(let ((n 100000000))
(time (loop repeat n do (add (rnd2) (rnd2))))
(time (loop repeat n do (add2 (rnd2) (rnd2))))
(print "...")
(time (loop repeat n do (add (rnd3) (rnd3))))
(time (loop repeat n do (add3 (rnd3) (rnd3))))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment