Skip to content

Commit

Permalink
Add time macro (#909)
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude authored Oct 30, 2023
1 parent a0ac138 commit baa0955
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/sci/impl/namespaces.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
print-method
print-dup
#?(:cljs alter-meta!)
memfn])
memfn
time])
(:require
#?(:clj [clojure.edn :as edn]
:cljs [cljs.reader :as edn])
Expand Down Expand Up @@ -986,6 +987,20 @@
#?(:clj (list 'new 'clojure.lang.LazySeq (list* '^{:once true} fn* [] body))
:cljs `(new cljs.core/LazySeq nil (fn [] ~@body) nil nil)))

(defn time
"Evaluates expr and prints the time it took. Returns the value of expr."
[_ _ expr]
`(let [start# (clojure.core/system-time)
ret# ~expr]
(prn (str "Elapsed time: "
#?(:clj (/ (double (- (clojure.core/system-time) start#)) 1000000.0)
:cljs (.toFixed (- (clojure.core/system-time) start#) 6))
" msecs"))
ret#))

#?(:clj (defn system-time []
(System/nanoTime)))

(macros/usetime

(def clojure-core
Expand Down Expand Up @@ -1489,10 +1504,13 @@
;; 'this-as (macrofy 'this-as this-as clojure-core-ns)])
'test (copy-core-var test)
'thread-bound? (copy-var sci-thread-bound? clojure-core-ns {:name 'thread-bound?})
'time (copy-var time clojure-core-ns {:macro true})
'subs (copy-core-var subs)
#?@(:clj ['supers (copy-core-var supers)])
'symbol (copy-var symbol* clojure-core-ns {:name 'symbol})
'symbol? (copy-core-var symbol?)
'system-time (copy-var #?(:clj system-time
:cljs system-time) clojure-core-ns)
'special-symbol? (copy-core-var special-symbol?)
'subvec (copy-core-var subvec)
'some-fn (copy-core-var some-fn)
Expand Down
12 changes: 12 additions & 0 deletions test/sci/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1666,6 +1666,18 @@
(deftest queue-test
(is (= #queue [1 2 3] (sci/eval-string "#queue [1 2 3]")))))

(deftest time-test
#?(:clj
(let [output (java.io.StringWriter.)]
(is (= 1 (sci/binding [sci/out output] (sci/eval-string "(time 1)"))))
(is (re-matches #"\"Elapsed time: \d\.\d+ msecs\"\s*" (str output))))
:cljs
(let [output (atom "")
print-fn #(swap! output str %)]
(is (= 1 (sci/binding [sci/print-fn print-fn] (sci/eval-string "(time 1)" {:classes {'js js/globalThis :allow :all}}))))
(is (re-matches #"\"Elapsed time: \d\.\d+ msecs\"\s*" @output))))
)

;;;; Scratch

(comment
Expand Down

0 comments on commit baa0955

Please sign in to comment.