bench: refactored bench function to make slightly less sucky

This commit is contained in:
hellerve 2017-11-06 20:47:21 +01:00
parent dfe57da99d
commit 395ff17574

View File

@ -23,52 +23,59 @@
()))
(Double.- (get-time-elapsed) start))))
; it is actually possible to make this run forever by supplying a _really_ longrunning
; function, where long-running is everything over 30ms.
(defn print-bench-results [res total]
(do
(print "Total time elapsed: " total)
(print "Best case: " (Statistics.Summary.min res))
(print "Worst case: " (Statistics.Summary.max res))
(print "Standard deviation: " (Statistics.Summary.stdev res))))
(defn get-samples [f n]
(let [zero 0.0
samples (Array.replicate 50 &zero)]
(do
(for [i 0 50]
(Array.aset! &samples i (Double./ (ns-iter-inner f (Double.to-int n)) n)))
(Statistics.summary &(Statistics.winsorize &samples 5.0)))))
(defn min-one [n]
(if (> 1.0 n) n 1.0))
; it is actually possible to make this run forever by supplying a _really_
; long-running function, where long-running is everything over 30ms.
(defn bench [f]
(let [ns (ns-iter-inner f 1)
ns-target-total 1000000.0
_n (Double./ ns-target-total (if (> 1.0 ns) 1.0 ns))
n (if (> 1.0 _n) 1.0 _n)
_n (Double./ ns-target-total (min-one ns))
n (min-one _n)
total 0.0
zero 0.0
samples (Array.replicate 50 &zero)
done false
res &(Statistics.summary &[0.0])]
(do
(while (and (Double.< total 3000000000.0) (not done))
(let [loop-start (get-time-elapsed)]
(do
(for [i 0 50]
(Array.aset! &samples i (Double./ (ns-iter-inner f (Double.to-int n)) n)))
(let [summ (Statistics.summary &(Statistics.winsorize &samples 5.0))]
(do
(for [i 0 50]
(Array.aset! &samples i (Double./ (ns-iter-inner f (Double.to-int n)) n)))
(let [summ5 (Statistics.summary &(Statistics.winsorize &samples 5.0))]
(let [loop-run (- (get-time-elapsed) loop-start)]
(if (and
(Double.> loop-run 100000.0)
(and
(Double.< (Statistics.Summary.median-abs-dev-pct &summ) 1.0)
(Double.< (Double.- (Statistics.Summary.median &summ)
(Statistics.Summary.median &summ5))
(Statistics.Summary.median-abs-dev &summ5))))
(do
(set! &total (Double.+ total loop-run))
(set! &done true)
(set! &res &summ5))
(do
(set! &total (Double.+ total loop-run))
(cond
(< (Double.* n 10.0) n) (set! &total (Double.+ total 3000000000.0)) ; abort
(set! &n (Double.* n 2.0))))))))))))
(let [loop-start (get-time-elapsed)
summ (get-samples f n)
summ5 (get-samples f n)
loop-run (- (get-time-elapsed) loop-start)]
(if (and
(Double.> loop-run 100000.0)
(and
(Double.< (Statistics.Summary.median-abs-dev-pct &summ) 1.0)
(Double.< (Double.- (Statistics.Summary.median &summ)
(Statistics.Summary.median &summ5))
(Statistics.Summary.median-abs-dev &summ5))))
(do
(set! &total (Double.+ total loop-run))
(set! &done true)
(set! &res &summ5))
(do
(set! &total (Double.+ total loop-run))
(if (< (Double.* n 10.0) n)
; abort on overflow
(set! &total (Double.+ total 3000000000.0))
(set! &n (Double.* n 2.0)))))))
(if done
(do
(print "Total time elapsed: " total)
(print "Best case: " (Statistics.Summary.min res))
(print "Worst case: " (Statistics.Summary.max res))
(print "Standard deviation: " (Statistics.Summary.stdev res)))
(print-bench-results res total)
(IO.println "Could not stabilize benchmark after more than 3 seconds!")))))
)