def
defines a global var in the current namespace. And defining global vars to time a function call is probably not a good idea. For example, let's say you want to measure the call (+ 1 2)
. You can use let
to do the timing (as opposed to def
), and use println
to print the time taken, as follows:
(let [start (System/nanoTime)
ret (+ 1 2)
end (System/nanoTime)]
(println (str "Elapsed time: " (- end start) " ns"))
ret)
clojure.core/time
captures this exact thing into a macro (with one difference, it prints the time taken in milliseconds). So, the above code would be equivalent to (time (+ 1 2))
. Check out (source time)
to see how that's done.