Fork me on GitHub
#clojurescript
<
2023-08-14
>
Lidor Cohen04:08:15

Is there an equivalent of time that returns time as data instead of printing it?

didibus04:08:51

(with-out-str
  (time ...))

🙏 2
grav07:08:59

Source is https://github.com/clojure/clojure/blob/clojure-1.10.1/src/clj/clojure/core.clj#L3884, so you could also snatch the part that generates the string

🙏 2
daveliepmann07:08:53

Miller, Halloway, & Bedra's Programming Clojure has a bench macro which does this

daveliepmann07:08:08

(defmacro bench [expr]
  `(let [start# (System/nanoTime)
         result# ~expr]
     {:result result#
      :elapsed (- (System/nanoTime) start#)}))

(comment
  (bench (str "a" "b"))
  ;; => {:elapsed 45125, :result "ab"}
  )

🙏 2