Skip to content

Commit 2fa41e7

Browse files
author
dnolen
committed
CLJS-1455: high resolution time
Added `cljs.core/system-time`, `time` macro now uses this instead
1 parent cc953d4 commit 2fa41e7

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,15 @@
333333

334334
(def DEMUNGE_PATTERN nil)
335335

336+
(defn system-time
337+
"Returns highest resolution time offered by host in milliseconds."
338+
[]
339+
(cond
340+
(exists? js/performance) (.now js/performance)
341+
(exists? js/process) (let [t (.hrtime js/process)]
342+
(/ (+ (* (aget t 0) 1e9) (aget t 1)) 1e6))
343+
:else (.getTime (js/Date.))))
344+
336345
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; arrays ;;;;;;;;;;;;;;;;
337346

338347
(defn ^array make-array

src/main/clojure/cljs/core.cljc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,9 +2505,11 @@
25052505
(core/defmacro time
25062506
"Evaluates expr and prints the time it took. Returns the value of expr."
25072507
[expr]
2508-
`(let [start# (.getTime (js/Date.))
2508+
`(let [start# (system-time)
25092509
ret# ~expr]
2510-
(prn (cljs.core/str "Elapsed time: " (- (.getTime (js/Date.)) start#) " msecs"))
2510+
(prn (cljs.core/str "Elapsed time: "
2511+
(.toFixed (- (system-time) start#) 6)
2512+
" msecs"))
25112513
ret#))
25122514

25132515
(core/defmacro simple-benchmark

0 commit comments

Comments
 (0)