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))
46 " Evaluates the expression n number of times, returning the average
47 time spent in computation, removing highest and lowest values.
49 If the body of expr returns nil, only the timing is returned otherwise
50 the result is printed - does not affect timing.
52 Before timings begin, a warmup is performed lasting either 1 minute or
53 1 full computational cycle, depending on which comes first."
54 [n expr] {:pre [(> n 2)]}
55 `(let [warm-up# (let [start# (System/currentTimeMillis)]
56 (println "Warming up!")
57 (while (< (System/currentTimeMillis) (+ start# (* 60 1000)))
60 (println "Benchmarking..."))
62 (for [pass# (range ~n)]
63 (let [start# (System/nanoTime)
65 timing# (/ (double (- (System/nanoTime) start#))
67 (when retr# (println retr#))
70 runtime# (reduce + timings#)
71 highest# (apply max timings#)
72 lowest# (apply min timings#)]
73 (println "Total runtime: " runtime#)
74 (println "Highest time : " highest#)
75 (println "Lowest time : " lowest#)
76 (println "Average : " (/ (- runtime# (+ highest# lowest#))
77 (- (count timings#) 2)))