Skip to content

Instantly share code, notes, and snippets.

@danownsthisspace
Created October 17, 2021 06:18
Show Gist options
  • Save danownsthisspace/5c41c9aa17fa57e2ce1528405dd85db9 to your computer and use it in GitHub Desktop.
Save danownsthisspace/5c41c9aa17fa57e2ce1528405dd85db9 to your computer and use it in GitHub Desktop.
(ns otca.shorts)
(defn wait-return [s r]
(Thread/sleep (* s 1000))
r)
#_(defn time-taken [expr]
(let [start (. System (nanoTime))
result (expr)
end (. System (nanoTime))
total-time (/ (double (- end start)) 100000.0)]
(println "Took " total-time " to eval")
result))
(defmacro time-taken [& expr]
`(let [start# (. System (nanoTime))
result# (do
~@expr)
end# (. System (nanoTime))
total-time# (/ (double (- end# start#)) 100000.0)]
(println '~expr " took " total-time# " to eval")
result#))
(comment
(def var1 "one")
`(list ~var1 2 3)
(wait-return 2 "hey")
(time-taken (+ 1 1))
(time-taken (+ 1 1) (wait-return 2 "hey"))
(macroexpand-1 '(time-taken (+ 1 1) (wait-return 2 "hey"))))
@jamiepratt
Copy link

100,000??

Nanoseconds are 10^-9 seconds.

@jamiepratt
Copy link

Thanks for linking to this code though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment