Created
February 7, 2019 15:19
-
-
Save mizunashi-mana/1de1f69f2723d7cef51c9a57c506fdcf to your computer and use it in GitHub Desktop.
お試し2 inspired by https://qiita.com/mod_poppo/items/03fc14f693b601e0a00f
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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