1
1
mirror of https://github.com/kanaka/mal.git synced 2024-10-27 14:52:16 +03:00
mal/impls/lib/perf.mal
Joel Martin 8a19f60386 Move implementations into impls/ dir
- Reorder README to have implementation list after "learning tool"
  bullet.

- This also moves tests/ and libs/ into impls. It would be preferrable
  to have these directories at the top level.  However, this causes
  difficulties with the wasm implementations which need pre-open
  directories and have trouble with paths starting with "../../". So
  in lieu of that, symlink those directories to the top-level.

- Move the run_argv_test.sh script into the tests directory for
  general hygiene.
2020-02-10 23:50:16 -06:00

42 lines
1.2 KiB
Plaintext

;; Mesure performances.
(load-file "../lib/load-file-once.mal")
(load-file-once "../lib/trivial.mal") ; gensym inc
;; Evaluate an expression, but report the time spent
(defmacro! time
(fn* (exp)
(let* [start (gensym)
ret (gensym)]
`(let* (~start (time-ms)
~ret ~exp)
(do
(println "Elapsed time:" (- (time-ms) ~start) "msecs")
~ret)))))
;; Count evaluations of a function during a given time frame.
(def! run-fn-for
(let* [
run-fn-for* (fn* [fn max-ms acc-ms last-iters]
(let* [start (time-ms)
_ (fn)
elapsed (- (time-ms) start)
iters (inc last-iters)
new-acc-ms (+ acc-ms elapsed)]
;; (do (prn "new-acc-ms:" new-acc-ms "iters:" iters))
(if (>= new-acc-ms max-ms)
last-iters
(run-fn-for* fn max-ms new-acc-ms iters))))
]
(fn* [fn max-secs]
;; fn : function without parameters
;; max-secs : number (seconds)
;; return : number (iterations)
(do
;; Warm it up first
(run-fn-for* fn 1000 0 0)
;; Now do the test
(run-fn-for* fn (* 1000 max-secs) 0 0)))))