Skip to content

Instantly share code, notes, and snippets.

@adamneilson
Created June 8, 2015 09:01
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 adamneilson/76a0366010b93d55f5a9 to your computer and use it in GitHub Desktop.
Save adamneilson/76a0366010b93d55f5a9 to your computer and use it in GitHub Desktop.
λ: (time (+ 2 2))
"Elapsed time: 0.02675 msecs"
4
λ: (timed (+ 2 2))
"Timed +: 0.066662 msecs"
4
λ: (source time)
(defmacro time
"Evaluates expr and prints the time it took. Returns the value of
expr."
{:added "1.0"}
[expr]
`(let [start# (. System (nanoTime))
ret# ~expr]
(prn (str "Elapsed time: " (/ (double (- (. System (nanoTime)) start#)) 1000000.0) " msecs"))
ret#))
nil
(defmacro get-funtion-name
[expr]
(prn (:name (meta (resolve expr)))))
λ: (get-funtion-name +)
+
nil
λ: (get-funtion-name (+ 2 2))
λ: ClassCastException clojure.lang.PersistentList cannot be cast to clojure.lang.Symbol clojure.core/ns-resolve
(defmacro better-get-funtion-name
[expr]
(let [sym (= (type expr) clojure.lang.Symbol)]
(prn (:name (meta (if sym
(resolve expr)
(resolve (first expr))))))))
λ: (better-get-funtion-name +)
+
nil
λ: (better-get-funtion-name (+ 2 2))
+
nil
(defmacro timed [expr]
(let [sym (= (type expr) clojure.lang.Symbol)]
`(let [start# (. System (nanoTime))
return# ~expr
res# (if ~sym
(resolve '~expr)
(resolve (first '~expr)))]
(prn (str "Timed "
(:name (meta res#))
": " (/ (double (- (. System (nanoTime)) start#)) 1000000.0) " msecs"))
return#)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment