IN: little-test
USING: fry locals continuations kernel math sequences system math.statistics ;
: isolated-time ( quote -- delta-in-millis )
{ } swap '[ millis @ millis ] with-datastack
[ first ] [ peek ] bi swap - ;
: faster? ( quot1 quot2 -- ? )
[ isolated-time ] bi@ < ;
! [ 2 3 + ] [ 3 4 - 1 seconds sleep ] faster?
! ==> t
:: bench-ave-time ( n quot -- avg ) ! (if n > 100,000, use 'drop' to drop the value at the end of your quot. )
SYMBOL: accum-vector
V{ } clone accum-vector set
12 [
[ n quot times ] isolated-time
accum-vector get swap suffix accum-vector set
] times
accum-vector get
natural-sort 1 tail 1 head* mean ;
: bench-timed. ( n quot -- )
[ bench-ave-time ]
[ drop pprint " operations took: " write >float pprint " milliseconds." write nl ] 2bi ;
! 300000 [ 3 4 * drop ] bench-ave-time.
! 300000 operations took: 31.2 milliseconds.
: bfaster? ( n quot1 quot2 -- ? )
pick swap [ bench-ave-time ] 2bi@ < ;