Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion src/Performance/Minibench.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
15 changes: 4 additions & 11 deletions src/Performance/Minibench.purs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,16 @@ 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.
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"
Expand Down Expand Up @@ -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
Expand Down