Created
February 4, 2014 03:38
-
-
Save fukamachi/8797790 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
I thought it at the beginning, but I threw the idea away because it doesn't work in these situations.
(aif (+ 1 1)
(let ((it 10))
(princ it))
'false)
(flet ((it () (princ "Yeah!")))
(aif (+ 1 1)
(it)
'false))
These are very trivial cases, though.
I'm voting against modifying if
. It is so basic construct, so I'd not expect it has any side effects. It is a good thing to have aif
yet. Maybe it is worth to rename it to if-it
to make the side-effect clearer?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how about it?