mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-19 01:18:40 +03:00
bench: refactored bench function to make slightly less sucky
This commit is contained in:
parent
dfe57da99d
commit
395ff17574
@ -23,30 +23,40 @@
|
|||||||
()))
|
()))
|
||||||
(Double.- (get-time-elapsed) start))))
|
(Double.- (get-time-elapsed) start))))
|
||||||
|
|
||||||
; it is actually possible to make this run forever by supplying a _really_ longrunning
|
(defn print-bench-results [res total]
|
||||||
; function, where long-running is everything over 30ms.
|
(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]
|
(defn bench [f]
|
||||||
(let [ns (ns-iter-inner f 1)
|
(let [ns (ns-iter-inner f 1)
|
||||||
ns-target-total 1000000.0
|
ns-target-total 1000000.0
|
||||||
_n (Double./ ns-target-total (if (> 1.0 ns) 1.0 ns))
|
_n (Double./ ns-target-total (min-one ns))
|
||||||
n (if (> 1.0 _n) 1.0 _n)
|
n (min-one _n)
|
||||||
total 0.0
|
total 0.0
|
||||||
zero 0.0
|
|
||||||
samples (Array.replicate 50 &zero)
|
|
||||||
done false
|
done false
|
||||||
res &(Statistics.summary &[0.0])]
|
res &(Statistics.summary &[0.0])]
|
||||||
(do
|
(do
|
||||||
(while (and (Double.< total 3000000000.0) (not done))
|
(while (and (Double.< total 3000000000.0) (not done))
|
||||||
(let [loop-start (get-time-elapsed)]
|
(let [loop-start (get-time-elapsed)
|
||||||
(do
|
summ (get-samples f n)
|
||||||
(for [i 0 50]
|
summ5 (get-samples f n)
|
||||||
(Array.aset! &samples i (Double./ (ns-iter-inner f (Double.to-int n)) n)))
|
loop-run (- (get-time-elapsed) loop-start)]
|
||||||
(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
|
(if (and
|
||||||
(Double.> loop-run 100000.0)
|
(Double.> loop-run 100000.0)
|
||||||
(and
|
(and
|
||||||
@ -60,15 +70,12 @@
|
|||||||
(set! &res &summ5))
|
(set! &res &summ5))
|
||||||
(do
|
(do
|
||||||
(set! &total (Double.+ total loop-run))
|
(set! &total (Double.+ total loop-run))
|
||||||
(cond
|
(if (< (Double.* n 10.0) n)
|
||||||
(< (Double.* n 10.0) n) (set! &total (Double.+ total 3000000000.0)) ; abort
|
; abort on overflow
|
||||||
(set! &n (Double.* n 2.0))))))))))))
|
(set! &total (Double.+ total 3000000000.0))
|
||||||
|
(set! &n (Double.* n 2.0)))))))
|
||||||
(if done
|
(if done
|
||||||
(do
|
(print-bench-results res total)
|
||||||
(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)))
|
|
||||||
(IO.println "Could not stabilize benchmark after more than 3 seconds!")))))
|
(IO.println "Could not stabilize benchmark after more than 3 seconds!")))))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user