A small collection of time measuring & benchmarking macros for Clojure.


Leiningen dependency:

[com.postspectacular/macrochrono "0.1.0"]

There are currently only two macro available to measure a single execution or to benchmark a number of iterations. In both cases the timings are given in milliseconds. timed-action returns a 2-element vector of the expression's result followed by its execution time.

(use 'macrochrono)
(timed-action (reduce + (range 1 100001)))
[5000050000 5.784]

The bench macro evaluates a given expression several times, discards the result and returns a map of execution time statistics:

(bench 1000 (reduce + (range 1 100001)))
{:median 5.1579999999999995, :max 9.091, :min 4.677, :avg 5.633388000000003}

Adding an additional "verbose" flag, each intermediate timing is printed to out.

(bench 10 (reduce + (range 1 100001)) true)
"Elapsed time: 6.021 msecs"
"Elapsed time: 8.251 msecs"
"Elapsed time: 5.162999999999999 msecs"
"Elapsed time: 5.269 msecs"
"Elapsed time: 6.8759999999999994 msecs"
"Elapsed time: 8.251999999999999 msecs"
"Elapsed time: 6.112 msecs"
"Elapsed time: 5.0169999999999995 msecs"
"Elapsed time: 6.927 msecs"
"Elapsed time: 5.637 msecs"
{:median 6.112, :max 8.251999999999999, :min 5.0169999999999995, :avg 6.3525}


Copyright © 2012 Karsten Schmidt

Distributed under the Eclipse Public License, the same as Clojure.