Skip to content

Instantly share code, notes, and snippets.

@priyadarshan
Forked from fukamachi/if-and-aif.lisp
Last active August 29, 2015 14:27
Show Gist options
  • Save priyadarshan/4209e505b3446933d538 to your computer and use it in GitHub Desktop.
Save priyadarshan/4209e505b3446933d538 to your computer and use it in GitHub Desktop.
(defmacro execution-time (&body body)
"Return the number of milliseconds it takes to execute `body`. Also
returns the result as the second value."
(let ((tm (gensym))
(res (gensym)))
`(let* ((,tm (get-internal-real-time))
(,res (progn ,@body))
(,tm (floor (* 1000 (- (get-internal-real-time) ,tm))
internal-time-units-per-second)))
(values (float ,tm) ,res))))
(defmacro aif (test then &optional else)
`(let ((it ,test))
(if it
,then
,else)))
(defun test-a ()
(aif (random 100)
'true
'false))
(defun test ()
(if (random 100)
'true
'false))
(let ((anaphoric (/ (loop repeat 5 sum (execution-time (loop repeat 10000000 do (test-a)))) 5))
(not-anaphoric (/ (loop repeat 5 sum (execution-time (loop repeat 10000000 do (test)))) 5)))
(format t "~&Anaphoric: ~A msec / Not-Anaphoric: ~A msec~%It's ~A % slower."
anaphoric
not-anaphoric
(* (/ (- anaphoric not-anaphoric) not-anaphoric) 100)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment