Skip to content

Instantly share code, notes, and snippets.

@mizunashi-mana
Created February 7, 2019 12:55
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/0de91c5f183ba4e40fe3cfcb9d0cb4b0 to your computer and use it in GitHub Desktop.
Save mizunashi-mana/0de91c5f183ba4e40fe3cfcb9d0cb4b0 to your computer and use it in GitHub Desktop.
module Bench.StateVSIORef where
import Data.IORef
import Control.Monad.State.Strict
n :: Int
n = 10000 * 10000
rotate :: [a] -> [a]
rotate [] = []
rotate (x:xs) = xs ++ [x]
someCalculationWithState :: IO [Ordering]
someCalculationWithState = flip evalStateT [LT, EQ, GT] $ do
forM_ [0..n] $ \_ -> do
modify' rotate
get
someCalculationWithIORef :: IO [Ordering]
someCalculationWithIORef = do
accRef <- newIORef [LT, EQ, GT]
forM_ [0..n] $ \_ -> do
modifyIORef' accRef rotate
readIORef accRef
someCalculationWithRec :: IO [Ordering]
someCalculationWithRec = return $ loop 0 [LT, EQ, GT]
where
loop i acc
| i == n = acc
| otherwise = loop (i + 1) $ rotate acc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment