Convert to cabal.project
[packages/containers.git] / containers-tests / benchmarks / Set.hs
1 {-# LANGUAGE BangPatterns #-}
2
3 module Main where
4
5 import Control.DeepSeq (rnf)
6 import Control.Exception (evaluate)
7 import Gauge (bench, defaultMain, whnf)
8 import Data.List (foldl')
9 import qualified Data.Set as S
10
11 main = do
12 let s = S.fromAscList elems :: S.Set Int
13 s_even = S.fromAscList elems_even :: S.Set Int
14 s_odd = S.fromAscList elems_odd :: S.Set Int
15 evaluate $ rnf [s, s_even, s_odd]
16 defaultMain
17 [ bench "member" $ whnf (member elems) s
18 , bench "insert" $ whnf (ins elems) S.empty
19 , bench "map" $ whnf (S.map (+ 1)) s
20 , bench "filter" $ whnf (S.filter ((== 0) . (`mod` 2))) s
21 , bench "partition" $ whnf (S.partition ((== 0) . (`mod` 2))) s
22 , bench "fold" $ whnf (S.fold (:) []) s
23 , bench "delete" $ whnf (del elems) s
24 , bench "findMin" $ whnf S.findMin s
25 , bench "findMax" $ whnf S.findMax s
26 , bench "deleteMin" $ whnf S.deleteMin s
27 , bench "deleteMax" $ whnf S.deleteMax s
28 , bench "unions" $ whnf S.unions [s_even, s_odd]
29 , bench "union" $ whnf (S.union s_even) s_odd
30 , bench "difference" $ whnf (S.difference s) s_even
31 , bench "intersection" $ whnf (S.intersection s) s_even
32 , bench "fromList" $ whnf S.fromList elems
33 , bench "fromList-desc" $ whnf S.fromList (reverse elems)
34 , bench "fromAscList" $ whnf S.fromAscList elems
35 , bench "fromDistinctAscList" $ whnf S.fromDistinctAscList elems
36 , bench "disjoint:false" $ whnf (S.disjoint s) s_even
37 , bench "disjoint:true" $ whnf (S.disjoint s_odd) s_even
38 , bench "null.intersection:false" $ whnf (S.null. S.intersection s) s_even
39 , bench "null.intersection:true" $ whnf (S.null. S.intersection s_odd) s_even
40 ]
41 where
42 elems = [1..2^12]
43 elems_even = [2,4..2^12]
44 elems_odd = [1,3..2^12]
45
46 member :: [Int] -> S.Set Int -> Int
47 member xs s = foldl' (\n x -> if S.member x s then n + 1 else n) 0 xs
48
49 ins :: [Int] -> S.Set Int -> S.Set Int
50 ins xs s0 = foldl' (\s a -> S.insert a s) s0 xs
51
52 del :: [Int] -> S.Set Int -> S.Set Int
53 del xs s0 = foldl' (\s k -> S.delete k s) s0 xs