Skip to content

Instantly share code, notes, and snippets.

@andrewthad
Last active June 8, 2018 13:12
Show Gist options
  • Save andrewthad/f79b7022725532baf709514cf08c3955 to your computer and use it in GitHub Desktop.
Save andrewthad/f79b7022725532baf709514cf08c3955 to your computer and use it in GitHub Desktop.
Strict foldMap benchmark
{-# LANGUAGE BangPatterns #-}
{-# OPTIONS_GHC -O2 #-}
import Gauge
import Data.Foldable
import qualified Data.Set as S
foldMap' :: (Monoid m, Foldable f) => (a -> m) -> f a -> m
foldMap' f = foldl' (\ !acc a -> acc <> f a) mempty
numbers :: [Int]
numbers = [1..4000]
intToSet :: Int -> S.Set Int
intToSet i = S.singleton (mod i 10)
main :: IO ()
main = defaultMain
[ bench "lazy" $ whnf (foldMap intToSet) numbers
, bench "strict" $ whnf (foldMap' intToSet) numbers
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment