Last active
June 8, 2018 13:12
-
-
Save andrewthad/f79b7022725532baf709514cf08c3955 to your computer and use it in GitHub Desktop.
Strict foldMap benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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