Skip to content

Instantly share code, notes, and snippets.

@swannodette
Created July 1, 2010 04:42
Show Gist options
  • Save swannodette/459585 to your computer and use it in GitHub Desktop.
Save swannodette/459585 to your computer and use it in GitHub Desktop.
(defmacro time2 [expr times]
`(time (dotimes [_# ~times] ~@expr)))
; -------------------------------------
(time2 (+ 1 2) 100)
; expands to ->
(let*
[start__3913__auto__
(. java.lang.System (nanoTime))
ret__3914__auto__
(let*
[n__3820__auto__ (int 100)]
(loop*
[___20964__auto__ (int 0)]
(if (< ___20964__auto__ n__3820__auto__)
(do + 1 2 (recur (unchecked-inc ___20964__auto__))))))]
(prn
(str
"Elapsed time: "
(/
(double (- (. java.lang.System (nanoTime)) start__3913__auto__))
1000000.0)
" msecs"))
ret__3914__auto__)
; which is what actually gets compiled
; COMPARE
(defmacro time2 [expr times]
(time `(dotimes [_# ~times] ~@expr)))
; -------------------------------------
(time2 (+ 1 2) 100)
; expands to ->
(let*
[n__3820__auto__ (int 100)]
(loop*
[___21242__auto__ (int 0)]
(if (< ___21242__auto__ n__3820__auto__)
(do + 1 2 (recur (unchecked-inc ___21242__auto__))))))
; note that the lack of any measurement of time in the expanded code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment