3 (defn square [x] (* x x))
6 "find absolute value of x"
22 (defn- find-divisor [n test-divisor]
23 (cond (> (square test-divisor) n) n
24 (divides? test-divisor n) test-divisor
25 :else (find-divisor n (inc test-divisor))))
27 (defn- smallest-divisor [n]
31 (= (smallest-divisor n) n))
38 (defn average [ & coll]
40 (float (count coll))))
42 (defn approx-equal [x y]
43 (< (abs (- x y)) 0.00001))
45 (defn error [^String string]
46 (throw (Exception. string)))
49 " Evaluates the expression n number of times, returning the average
50 time spent in computation, removing highest and lowest values.
52 If the body of expr returns nil, only the timing is returned otherwise
53 the result is printed - does not affect timing.
55 Before timings begin, a warmup is performed lasting either 1 minute or
56 1 full computational cycle, depending on which comes first."
57 [n expr] {:pre [(> n 2)]}
58 `(let [warm-up# (let [start# (System/currentTimeMillis)]
59 (println "Warming up!")
60 (while (< (System/currentTimeMillis) (+ start# (* 60 1000)))
63 (println "Benchmarking..."))
65 (for [pass# (range ~n)]
66 (let [start# (System/nanoTime)
68 timing# (/ (double (- (System/nanoTime) start#))
70 (when retr# (println retr#))
73 runtime# (reduce + timings#)
74 highest# (apply max timings#)
75 lowest# (apply min timings#)]
76 (println "Total runtime: " runtime#)
77 (println "Highest time : " highest#)
78 (println "Lowest time : " lowest#)
79 (println "Average : " (/ (- runtime# (+ highest# lowest#))
80 (- (count timings#) 2)))