Skip to content

Instantly share code, notes, and snippets.

@chessai
Created July 30, 2018 23:25
Show Gist options
  • Save chessai/47eef4c45487c8d04f21edad4736ffec to your computer and use it in GitHub Desktop.
Save chessai/47eef4c45487c8d04f21edad4736ffec to your computer and use it in GitHub Desktop.
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -O2 -fforce-recomp #-}
module Main (main) where
import Criterion.Main
import Control.Monad
import Data.Int
import Data.Vector (Vector)
import qualified Data.Vector as Vector
import Control.DeepSeq ( ($!!) )
main :: IO ()
main = do
bs :: Vector Int <- pure $!! Vector.fromList [1..5000]
defaultMain
[ bgroup "fmap bench"
[ bench "FMAP (LAZY)" $ nf (fmap (+1)) bs
, bench "FMAP (STRICT)" $ nf (fmap' (+1)) bs
]
]
fmap' :: Monad m => (a -> b) -> m a -> m b
fmap' f m = m >>= \ !x -> pure $! f x
{-# INLINE fmap' #-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment