Timing expressions and comparing results

32 views
Skip to first unread message

Frantisek Sodomka

unread,
Jun 29, 2013, 7:15:26 AM6/29/13
to clo...@googlegroups.com
Hi all,

If you need to compare running times of different expressions, you could use 'timings' macro:
https://gist.github.com/fsodomka/5890711

It takes the number of runs and expressions to time. For example:
user=> (timings 1e7 (+ 1 2 3 4) (+ 1 (+ 2 (+ 3 4))))
[{:time 55.028223, :expr (+ 1 2 3 4)} {:time 55.738369, :expr (+ 1 (+ 2 (+ 3 4)))}]

Function 'report' prints out comparison of timings. For it to work, function 'clojure.pprint/print-table' is needed (Clojure 1.3+).

Results of expressions must be equal (=), otherwise an exception is thrown:
user=> (timings 1e7 (+ 100 2 3 4) (+ 1 (+ 2 (+ 3 4))))
Exception Results of expressions are not equal  user/eval1153 (NO_SOURCE_FILE:354)

I hope you find it useful and it helps your programs to run faster,
Frantisek

Reply all
Reply to author
Forward
0 new messages