Skip to content

Commit 63f8004

Browse files
committed
add BenchResult
1 parent 56f6300 commit 63f8004

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/Performance/Minibench.purs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
module Performance.Minibench
88
( bench
99
, benchWith
10+
, benchWith'
11+
, BenchResult
12+
, withUnits
1013
) where
1114

1215
import Control.Monad.Eff (Eff, forE)
@@ -50,7 +53,26 @@ benchWith
5053
. Int
5154
-> (Unit -> a)
5255
-> Eff (console :: CONSOLE | eff) Unit
53-
benchWith n f = runST do
56+
benchWith n f = do
57+
res <- benchWith' n f
58+
log ("mean = " <> withUnits res.mean)
59+
log ("stddev = " <> withUnits res.stdDev)
60+
log ("min = " <> withUnits res.min)
61+
log ("max = " <> withUnits res.max)
62+
63+
type BenchResult =
64+
{ mean :: Number
65+
, stdDev :: Number
66+
, min :: Number
67+
, max :: Number
68+
}
69+
70+
benchWith'
71+
:: forall eff a
72+
. Int
73+
-> (Unit -> a)
74+
-> Eff eff BenchResult
75+
benchWith' n f = runST do
5476
sumRef <- newSTRef 0.0
5577
sum2Ref <- newSTRef 0.0
5678
minRef <- newSTRef infinity
@@ -73,10 +95,12 @@ benchWith n f = runST do
7395
let n' = toNumber n
7496
mean = sum / n'
7597
stdDev = sqrt ((sum2 - n' * mean * mean) / (n' - 1.0))
76-
log ("mean = " <> withUnits mean)
77-
log ("stddev = " <> withUnits stdDev)
78-
log ("min = " <> withUnits min')
79-
log ("max = " <> withUnits max')
98+
pure
99+
{ mean
100+
, stdDev
101+
, min: min'
102+
, max: max'
103+
}
80104

81105
-- | Estimate the running time of a function and print a summary to the console,
82106
-- | by running the function 1000 times.

0 commit comments

Comments
 (0)