File: ref.hs

package info (click to toggle)
haskell-mutable-containers 0.3.4.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 128 kB
  • sloc: haskell: 888; makefile: 3
file content (30 lines) | stat: -rw-r--r-- 746 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{-# LANGUAGE TypeFamilies #-}
import Control.Monad
import Gauge.Main
import Data.Mutable

test :: (MCState c ~ PrimState IO, RefElement c ~ Int, MutableRef c)
     => String
     -> (c -> c)
     -> Benchmark
test name forceType = bench name $ whnfIO $ do
    ref <- fmap forceType $ newRef (5 :: Int)
    replicateM_ 500 $ do
        modifyRef' ref (+ 1)
        modifyRef' ref (subtract 1)
        void $ readRef ref
    replicateM_ 500 $ do
        writeRef ref (5 :: Int)
        void $ readRef ref
{-# INLINE test #-}

main :: IO ()
main = defaultMain
    [ test "IORef" asIORef
    , test "STRef" asSTRef
    , test "MutVar" asMutVar
    , test "URef" asURef
    , test "PRef" asPRef
    , test "SRef" asSRef
    , test "BRef" asBRef
    ]