diff --git a/CHANGELOG.md b/CHANGELOG.md index 585fa1f..25c0e53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ New features: Bugfixes: Other improvements: +- Update FFI to use a more direct and portable convention (#25 by @natefaubion) ## [v4.0.0](https://github.com/purescript/purescript-minibench/releases/tag/v4.0.0) - 2022-04-27 diff --git a/src/Performance/Minibench.js b/src/Performance/Minibench.js index f5e8314..3f236ca 100644 --- a/src/Performance/Minibench.js +++ b/src/Performance/Minibench.js @@ -1,4 +1,9 @@ -export const hrTime = process.hrtime; +export function timeNs(k) { + const t1 = process.hrtime(); + k(); + const t2 = process.hrtime(t1); + return t2[0] * 1.0e9 + t2[1]; +} export function gc() { global.gc && global.gc(); diff --git a/src/Performance/Minibench.purs b/src/Performance/Minibench.purs index aa46125..c0e63fd 100644 --- a/src/Performance/Minibench.purs +++ b/src/Performance/Minibench.purs @@ -20,10 +20,9 @@ import Effect.Console (log) import Effect.Ref as Ref import Effect.Uncurried (EffectFn1, runEffectFn1) import Data.Number (infinity, max, min, sqrt) -import Partial.Unsafe (unsafeCrashWith) --- | A wrapper around the Node `process.hrtime()` function. -foreign import hrTime :: EffectFn1 (Array Int) (Array Int) +-- | Returns the number of nanoseconds it takes to evaluate the given closure. +foreign import timeNs :: forall a. EffectFn1 (Unit -> a) Number -- | Force garbage collection. -- | Requires node to be run with the --force-gc flag. @@ -31,10 +30,6 @@ foreign import gc :: Effect Unit foreign import toFixed :: Number -> String -fromHrTime :: Array Int -> Number -fromHrTime [s, ns] = toNumber s * 1.0e9 + toNumber ns -fromHrTime _ = unsafeCrashWith "fromHrTime: unexpected result from process.hrtime()" - withUnits :: Number -> String withUnits t | t < 1.0e3 = toFixed t <> " ns" @@ -79,10 +74,8 @@ benchWith' n f = do maxRef <- Ref.new 0.0 gc forE 0 n \_ -> do - t1 <- runEffectFn1 hrTime [0, 0] - t2 <- const (runEffectFn1 hrTime t1) (f unit) - let ns = fromHrTime t2 - square = ns * ns + ns <- runEffectFn1 timeNs f + let square = ns * ns _ <- Ref.modify (_ + ns) sumRef _ <- Ref.modify (_ + square) sum2Ref _ <- Ref.modify (_ `min` ns) minRef