@@ -12,22 +12,23 @@ module Performance.Minibench
1212 , withUnits
1313 ) where
1414
15- import Control.Monad.Eff (Eff , forE )
16- import Control.Monad.Eff.Console (CONSOLE , log )
17- import Control.Monad.Eff.Uncurried (EffFn1 , runEffFn1 )
18- import Control.Monad.ST (modifySTRef , newSTRef , readSTRef , runST )
15+ import Prelude hiding (min ,max )
16+
1917import Data.Int (toNumber )
18+ import Effect (Effect , forE )
19+ import Effect.Console (log )
20+ import Effect.Ref as Ref
21+ import Effect.Uncurried (EffectFn1 , runEffectFn1 )
2022import Global (infinity )
2123import Math (max , min , sqrt )
2224import Partial.Unsafe (unsafeCrashWith )
23- import Prelude hiding (min ,max )
2425
2526-- | A wrapper around the Node `process.hrtime()` function.
26- foreign import hrTime :: forall eff . EffFn1 eff (Array Int ) (Array Int )
27+ foreign import hrTime :: EffectFn1 (Array Int ) (Array Int )
2728
2829-- | Force garbage collection.
2930-- | Requires node to be run with the --force-gc flag.
30- foreign import gc :: forall eff . Eff eff Unit
31+ foreign import gc :: Effect Unit
3132
3233foreign import toFixed :: Number -> String
3334
@@ -49,49 +50,49 @@ withUnits t
4950-- | To increase benchmark accuracy by forcing garbage collection before the
5051-- | benchmark is run, node should be invoked with the '--expose-gc' flag.
5152benchWith
52- :: forall eff a
53+ :: forall a
5354 . Int
5455 -> (Unit -> a )
55- -> Eff ( console :: CONSOLE | eff ) Unit
56+ -> Effect Unit
5657benchWith n f = do
5758 res <- benchWith' n f
5859 log (" mean = " <> withUnits res.mean)
5960 log (" stddev = " <> withUnits res.stdDev)
6061 log (" min = " <> withUnits res.min)
6162 log (" max = " <> withUnits res.max)
6263
63- type BenchResult =
64+ type BenchResult =
6465 { mean :: Number
6566 , stdDev :: Number
6667 , min :: Number
6768 , max :: Number
6869 }
6970
7071benchWith'
71- :: forall eff a
72+ :: forall a
7273 . Int
7374 -> (Unit -> a )
74- -> Eff eff BenchResult
75- benchWith' n f = runST do
76- sumRef <- newSTRef 0.0
77- sum2Ref <- newSTRef 0.0
78- minRef <- newSTRef infinity
79- maxRef <- newSTRef 0.0
75+ -> Effect BenchResult
76+ benchWith' n f = do
77+ sumRef <- Ref .new 0.0
78+ sum2Ref <- Ref .new 0.0
79+ minRef <- Ref .new infinity
80+ maxRef <- Ref .new 0.0
8081 gc
8182 forE 0 n \_ -> do
82- t1 <- runEffFn1 hrTime [0 , 0 ]
83- t2 <- const (runEffFn1 hrTime t1) (f unit)
83+ t1 <- runEffectFn1 hrTime [0 , 0 ]
84+ t2 <- const (runEffectFn1 hrTime t1) (f unit)
8485 let ns = fromHrTime t2
8586 square = ns * ns
86- _ <- modifySTRef sumRef (_ + ns)
87- _ <- modifySTRef sum2Ref (_ + square)
88- _ <- modifySTRef minRef (_ `min` ns)
89- _ <- modifySTRef maxRef (_ `max` ns)
87+ _ <- Ref .modify (_ + ns) sumRef
88+ _ <- Ref .modify (_ + square) sum2Ref
89+ _ <- Ref .modify (_ `min` ns) minRef
90+ _ <- Ref .modify (_ `max` ns) maxRef
9091 pure unit
91- sum <- readSTRef sumRef
92- sum2 <- readSTRef sum2Ref
93- min' <- readSTRef minRef
94- max' <- readSTRef maxRef
92+ sum <- Ref .read sumRef
93+ sum2 <- Ref .read sum2Ref
94+ min' <- Ref .read minRef
95+ max' <- Ref .read maxRef
9596 let n' = toNumber n
9697 mean = sum / n'
9798 stdDev = sqrt ((sum2 - n' * mean * mean) / (n' - 1.0 ))
@@ -116,8 +117,5 @@ benchWith' n f = runST do
116117-- | mean = 414.00 μs
117118-- | stddev = 494.82 μs
118119-- | ```
119- bench
120- :: forall eff a
121- . (Unit -> a )
122- -> Eff (console :: CONSOLE | eff ) Unit
120+ bench :: forall a . (Unit -> a ) -> Effect Unit
123121bench = benchWith 1000
0 commit comments