Skip to content

Instantly share code, notes, and snippets.

@mizunashi-mana
Created February 8, 2019 07:49
Show Gist options
  • Select an option

  • Save mizunashi-mana/176875a0af98578d0d9bafd2dabdf876 to your computer and use it in GitHub Desktop.

Select an option

Save mizunashi-mana/176875a0af98578d0d9bafd2dabdf876 to your computer and use it in GitHub Desktop.
お試し with unboxed-ref inspired by https://qiita.com/mod_poppo/items/03fc14f693b601e0a00f
{-# LANGUAGE LambdaCase #-}
module Bench.StateVSIORef where
import Data.IORef
import Data.IORef.Unboxed
import Control.Monad.State.Strict
n :: Int
n = 2 ^ (30 :: Int)
type Acc = Int
f :: Int -> Acc -> Acc
f i acc = acc + (i `rem` 3)
e :: Acc
e = 0
someCalculationWithState :: IO Acc
someCalculationWithState = flip evalStateT e $ do
forM_ [0..n] $ \i -> do
modify' (f i)
get
someCalculationWithIORef :: IO Acc
someCalculationWithIORef = do
accRef <- newIORef e
forM_ [0..n] $ \i -> do
modifyIORef' accRef (f i)
readIORef accRef
someCalculationWithIORefU :: IO Acc
someCalculationWithIORefU = do
accRef <- newIORefU e
forM_ [0..n] $ \i -> do
modifyIORefU accRef (f i)
readIORefU accRef
someCalculationWithRec :: IO Acc
someCalculationWithRec = return $ loop 0 e
where
loop i acc
| i == n = f i acc
| otherwise = loop (i + 1) $ f i acc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment