time

added
1.0

ns
clojure.core

type
macro

(time expr)

Evaluates expr and prints the time it took.  Returns the value of
expr.

                user> (time (Thread/sleep 100))
"Elapsed time: 100.284772 msecs"
nil
            
                ;when working with lazy seqs
(time (doall (...)))
            
                ;; Time how long it takes to write a string to a file 100 times
(defn time-test []
  (with-open [w (writer "test.txt" :append false)]
    (dotimes [_ 100]
      (.write w "I am being written to a file."))))


user=> (time (time-test))
"Elapsed time: 19.596371 msecs"
            
                user=> (time (Thread/sleep 1000))
"Elapsed time: 1000.267483 msecs"
nil
user=> (with-out-str (time (Thread/sleep 1000)))
"\\"Elapsed time: 1010.12942 msecs\\"\
"