Skip to content

Commit 56f6300

Browse files
authored
Merge pull request #3 from matthewleon/gc
run gc before benching
2 parents 9727a82 + 4ee2cf8 commit 56f6300

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/Performance/Minibench.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
exports.hrTime = process.hrtime;
44

5+
exports.gc = function() {
6+
global.gc && global.gc();
7+
};
8+
59
exports.toFixed = function(n) {
610
return n.toFixed(2);
711
};

src/Performance/Minibench.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import Prelude hiding (min,max)
2222
-- | A wrapper around the Node `process.hrtime()` function.
2323
foreign import hrTime :: forall eff. EffFn1 eff (Array Int) (Array Int)
2424

25+
-- | Force garbage collection.
26+
-- | Requires node to be run with the --force-gc flag.
27+
foreign import gc :: forall eff. Eff eff Unit
28+
2529
foreign import toFixed :: Number -> String
2630

2731
fromHrTime :: Array Int -> Number
@@ -38,6 +42,9 @@ withUnits t
3842
-- | Estimate the running time of a function and print a summary to the console,
3943
-- | specifying the number of samples to take. More samples will give a better
4044
-- | estimate of both mean and standard deviation, but will increase running time.
45+
-- |
46+
-- | To increase benchmark accuracy by forcing garbage collection before the
47+
-- | benchmark is run, node should be invoked with the '--expose-gc' flag.
4148
benchWith
4249
:: forall eff a
4350
. Int
@@ -48,6 +55,7 @@ benchWith n f = runST do
4855
sum2Ref <- newSTRef 0.0
4956
minRef <- newSTRef infinity
5057
maxRef <- newSTRef 0.0
58+
gc
5159
forE 0 n \_ -> do
5260
t1 <- runEffFn1 hrTime [0, 0]
5361
t2 <- const (runEffFn1 hrTime t1) (f unit)

0 commit comments

Comments
 (0)