Skip to content

Instantly share code, notes, and snippets.

@mizunashi-mana
Created February 7, 2019 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizunashi-mana/1de1f69f2723d7cef51c9a57c506fdcf to your computer and use it in GitHub Desktop.
Save mizunashi-mana/1de1f69f2723d7cef51c9a57c506fdcf to your computer and use it in GitHub Desktop.
{-# 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