Skip to content

Instantly share code, notes, and snippets.

@myuon
Last active April 20, 2019 14:26
Show Gist options
  • Save myuon/c9f4cbcb46fa87529d32b49d75134043 to your computer and use it in GitHub Desktop.
Save myuon/c9f4cbcb46fa87529d32b49d75134043 to your computer and use it in GitHub Desktop.
import Gauge.Main
import Control.Monad
import Control.Monad.State.Strict
import Data.IORef
import qualified Data.Vector.Unboxed.Mutable as V
import Data.Primitive.MutVar
{-
benchmarked IORef
time 3.196 μs (3.170 μs .. 3.226 μs)
0.996 R² (0.989 R² .. 1.000 R²)
mean 3.230 μs (3.205 μs .. 3.293 μs)
std dev 120.7 ns (54.65 ns .. 225.7 ns)
variance introduced by outliers: 20% (moderately inflated)
benchmarked vector
time 1.374 μs (1.370 μs .. 1.377 μs)
1.000 R² (1.000 R² .. 1.000 R²)
mean 1.372 μs (1.370 μs .. 1.374 μs)
std dev 6.750 ns (5.682 ns .. 8.349 ns)
benchmarked mutvar
time 3.212 μs (3.194 μs .. 3.235 μs)
0.999 R² (0.998 R² .. 1.000 R²)
mean 3.211 μs (3.201 μs .. 3.226 μs)
std dev 41.10 ns (27.42 ns .. 66.97 ns)
benchmarked state
time 1.776 ns (1.771 ns .. 1.781 ns)
1.000 R² (1.000 R² .. 1.000 R²)
mean 1.777 ns (1.774 ns .. 1.780 ns)
std dev 11.01 ps (9.129 ps .. 13.57 ps)
-}
main = do
ref <- newIORef (0 :: Int)
vec <- V.replicate 1 (0 :: Int)
mvar <- newMutVar (0 :: Int)
defaultMain
[ bench "IORef" $ nfIO $ replicateM_ 1000 $ modifyIORef' ref (+ 1)
, bench "vector" $ nfIO $ replicateM_ 1000 $ V.modify vec (+ 1) 0
, bench "mutvar" $ nfIO $ replicateM_ 1000 $ modifyMutVar' mvar (+ 1)
, bench "state"
$ nfIO
$ return
$ flip execState (0 :: Int)
$ replicateM_ 1000
$ modify' (+ 1)
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment