Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
{-# LANGUAGE LambdaCase #-}
module Bench.StateVSIORef where
import Data.IORef
import Control.Monad.State.Strict
n :: Int
n = 2 ^ (31 :: Int)
type Acc = Either () Ordering
f :: Acc -> Acc
f = \case
Left _ -> Right EQ
Right EQ -> Right LT
Right LT -> Right GT
Right GT -> Left ()
e :: Acc
e = Right LT
someCalculationWithState :: IO Acc
someCalculationWithState = flip evalStateT e $ do
forM_ [0..n] $ \_ -> do
modify' f
get
someCalculationWithIORef :: IO Acc
someCalculationWithIORef = do
accRef <- newIORef e
forM_ [0..n] $ \_ -> do
modifyIORef' accRef f
readIORef accRef
someCalculationWithRec :: IO Acc
someCalculationWithRec = return $ loop 0 e
where
loop i acc
| i == n = f acc
| otherwise = loop (i + 1) $ f acc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment