on master help sign in
Add ST reference to the countdown benchmark
b1d8d7aAndrzej Rybczak
bench/Countdown.hs+22
@@ -1,7 +1,9 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2module Countdown where
2module Countdown where
3
3
4+import Control.Monad.ST
4import Data.Functor.Identity
5import Data.Functor.Identity
6+import Data.STRef
5
7
6-- eff
8-- eff
7#ifdef VERSION_eff
9#ifdef VERSION_eff
@@ -47,6 +49,26 @@ countdownRef :: Integer -> (Integer, Integer)
47countdownRef n = if n <= 0 then (n, n) else countdownRef $ n - 1
49countdownRef n = if n <= 0 then (n, n) else countdownRef $ n - 1
48{-# NOINLINE countdownRef #-}
50{-# NOINLINE countdownRef #-}
49
51
52+----------------------------------------
53+-- ST
54+
55+programST :: STRef s Integer -> ST s Integer
56+programST ref = do
57+ n <- readSTRef ref
58+ if n <= 0
59+ then pure n
60+ else do
61+ writeSTRef ref $! n - 1
62+ programST ref
63+{-# NOINLINE programST #-}
64+
65+countdownST :: Integer -> (Integer, Integer)
66+countdownST n = runST $ do
67+ ref <- newSTRef n
68+ a <- programST ref
69+ s <- readSTRef ref
70+ pure (a, s)
71+
50----------------------------------------
72----------------------------------------
51-- mtl
73-- mtl
52
74
bench/Main.hs+2−1
@@ -22,7 +22,8 @@ main = defaultMain
22countdown :: Integer -> Benchmark
22countdown :: Integer -> Benchmark
23countdown n = bgroup (show n)
23countdown n = bgroup (show n)
24 [ bgroup "shallow"
24 [ bgroup "shallow"
25− [ bench "reference" $ nf countdownRef n
25+ [ bench "reference (pure)" $ nf countdownRef n
26+ , bench "reference (ST)" $ nf countdownST n
26 , bench "effectful (pure-static)" $ nf countdownEffectfulStatic n
27 , bench "effectful (pure-static)" $ nf countdownEffectfulStatic n
27 , bench "effectful (pure-dynamic)" $ nf countdownEffectfulDynPure n
28 , bench "effectful (pure-dynamic)" $ nf countdownEffectfulDynPure n
28 , bench "effectful (MVar-static)" $ nf countdownEffectfulMVar n
29 , bench "effectful (MVar-static)" $ nf countdownEffectfulMVar n