I'm new to Clojure and having some trouble creating a macro.
Code in Clojure that evaluates the sum of the first 1,000,000 integers:
(apply + (range 1E6))
This benchmark code evaluates the sum, but also prints the number of nanoseconds this code takes:
(let [start (System/nanoTime)
result (apply + (range 1E6))]
end (System/nanoTime)]
(println "Took:" (- end start) "ns")
result)
How would I go about creating a macro in the following form meeting s.t. the value of (benchmark expr) is the evaluation of expr AND it prints the time it takes to evaluate expr in nanoseconds?
(defmacro benchmark [code]
...)
Also, I'm a bit confused as to how macros work in general. Why can't we use an ordinary function in the following form?
(defn benchmark [code]
...)
time, which does exactly that. - glts(criterium/bench (apply + (range 1E6))). - Carcigenicate