@@ -20,21 +20,16 @@ import Effect.Console (log)
2020import Effect.Ref as Ref
2121import Effect.Uncurried (EffectFn1 , runEffectFn1 )
2222import Data.Number (infinity , max , min , sqrt )
23- import Partial.Unsafe (unsafeCrashWith )
2423
25- -- | A wrapper around the Node `process.hrtime()` function .
26- foreign import hrTime :: EffectFn1 ( Array Int ) ( Array Int )
24+ -- | Returns the number of nanoseconds it takes to evaluate the given closure .
25+ foreign import timeNs :: forall a . EffectFn1 ( Unit -> a ) Number
2726
2827-- | Force garbage collection.
2928-- | Requires node to be run with the --force-gc flag.
3029foreign import gc :: Effect Unit
3130
3231foreign import toFixed :: Number -> String
3332
34- fromHrTime :: Array Int -> Number
35- fromHrTime [s, ns] = toNumber s * 1.0e9 + toNumber ns
36- fromHrTime _ = unsafeCrashWith " fromHrTime: unexpected result from process.hrtime()"
37-
3833withUnits :: Number -> String
3934withUnits t
4035 | t < 1.0e3 = toFixed t <> " ns"
@@ -79,10 +74,8 @@ benchWith' n f = do
7974 maxRef <- Ref .new 0.0
8075 gc
8176 forE 0 n \_ -> do
82- t1 <- runEffectFn1 hrTime [0 , 0 ]
83- t2 <- const (runEffectFn1 hrTime t1) (f unit)
84- let ns = fromHrTime t2
85- square = ns * ns
77+ ns <- runEffectFn1 timeNs f
78+ let square = ns * ns
8679 _ <- Ref .modify (_ + ns) sumRef
8780 _ <- Ref .modify (_ + square) sum2Ref
8881 _ <- Ref .modify (_ `min` ns) minRef
0 commit comments