Skip to content

Instantly share code, notes, and snippets.

@minoki
Created April 20, 2019 14:58
Show Gist options
  • Save minoki/bb9bd009bb5fc98f4f5b40333b02ef05 to your computer and use it in GitHub Desktop.
Save minoki/bb9bd009bb5fc98f4f5b40333b02ef05 to your computer and use it in GitHub Desktop.
{-# LANGUAGE BangPatterns #-}
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 vector
time 2.296 μs (2.276 μs .. 2.312 μs)
1.000 R² (0.999 R² .. 1.000 R²)
mean 2.278 μs (2.270 μs .. 2.289 μs)
std dev 32.60 ns (23.98 ns .. 45.74 ns)
benchmarked vector unsafe
time 2.227 μs (2.215 μs .. 2.244 μs)
1.000 R² (0.999 R² .. 1.000 R²)
mean 2.237 μs (2.230 μs .. 2.244 μs)
std dev 26.40 ns (22.24 ns .. 32.02 ns)
benchmarked strict state with nf
time 759.4 ns (752.3 ns .. 768.7 ns)
0.999 R² (0.998 R² .. 0.999 R²)
mean 765.6 ns (760.9 ns .. 770.5 ns)
std dev 16.54 ns (13.49 ns .. 20.76 ns)
benchmarked strict state with nfAppIO
time 705.0 ns (700.8 ns .. 709.4 ns)
0.998 R² (0.996 R² .. 1.000 R²)
mean 713.3 ns (709.4 ns .. 721.3 ns)
std dev 18.99 ns (13.16 ns .. 28.59 ns)
variance introduced by outliers: 11% (moderately inflated)
-}
main = do
vec <- V.replicate 1 (0 :: Int)
defaultMain
[ bench "vector" $ nfAppIO (\n -> do
V.write vec 0 0
replicateM_ n $ V.modify vec (+ 1) 0
) 1000
, bench "vector unsafe" $ nfAppIO (\n -> do
V.write vec 0 0
replicateM_ n $ V.unsafeModify vec (+ 1) 0
) 1000
, bench "strict state with nf"
$ nf (\n -> do
flip execState (0 :: Int)
$ replicateM_ n
$ modify' (+ 1)
) 1000
, bench "strict state with nfAppIO"
$ nfAppIO (\n -> do let !result = flip execState (0 :: Int)
$ replicateM_ n
$ modify' (+ 1)
return result
) 1000
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment