on a3203ff help sign in
111 lines 3.3 kB history raw
1{-# LANGUAGE CPP #-}
3#include "Streamly/Benchmark/Data/Array/CommonImports.hs"
5import Control.DeepSeq (deepseq)
7import qualified Streamly.Internal.Data.Array as IA
8import qualified Streamly.Internal.Data.Array as A
9type Stream = A.Array
11#include "Streamly/Benchmark/Data/Array/Common.hs"
13-------------------------------------------------------------------------------
14-- Benchmark helpers
15-------------------------------------------------------------------------------
17-- Drain a source that generates an array in the IO monad
18{-# INLINE benchIOSrc #-}
19benchIOSrc :: NFData a => String -> (Int -> IO (Stream a)) -> Benchmark
20benchIOSrc name src = benchIO name src id
22-------------------------------------------------------------------------------
23-- Bench Ops
24-------------------------------------------------------------------------------
26{-# INLINE sourceIntFromToFromList #-}
27sourceIntFromToFromList :: MonadIO m => Int -> Int -> m (Stream Int)
28sourceIntFromToFromList value n = P.return $ A.fromListN value [n..n + value]
30{-# INLINE readInstance #-}
31readInstance :: P.String -> Stream Int
32readInstance str =
33 let r = P.reads str
34 in case r of
35 [(x,"")] -> x
36 _ -> P.error "readInstance: no parse"
38#ifdef DEVBUILD
39{-
40{-# INLINE foldableFoldl' #-}
41foldableFoldl' :: Stream Int -> Int
42foldableFoldl' = F.foldl' (+) 0
44{-# INLINE foldableSum #-}
45foldableSum :: Stream Int -> Int
46foldableSum = P.sum
47-}
48#endif
50{-# INLINE sourceIntFromToFromStream #-}
51sourceIntFromToFromStream :: MonadIO m => Int -> Int -> m (Stream Int)
52sourceIntFromToFromStream value n =
53 S.fold A.write $ S.enumerateFromTo n (n + value)
55-------------------------------------------------------------------------------
56-- Bench groups
57-------------------------------------------------------------------------------
59o_1_space_generation :: Int -> [Benchmark]
60o_1_space_generation value =
61 [ bgroup
62 "generation"
63 [ benchIOSrc "write . intFromTo" (sourceIntFromToFromStream value)
64 , let testStr = "fromList " ++ mkListString value
65 in testStr `deepseq` bench "read" (nf readInstance testStr)
66 ]
67 ]
69o_1_space_elimination :: Int -> [Benchmark]
70o_1_space_elimination value =
71 [ bgroup "elimination"
72 [ benchFold "writeLastN.1"
73 (S.fold (IA.writeLastN 1)) (P.sourceUnfoldrM value)
74 , benchFold "writeLastN.10"
75 (S.fold (IA.writeLastN 10)) (P.sourceUnfoldrM value)
76#ifdef DEVBUILD
77{-
78 benchPureSink value "foldable/foldl'" foldableFoldl'
79 , benchPureSink value "foldable/sum" foldableSum
80-}
81#endif
82 ]
83 ]
85o_n_heap_serial :: Int -> [Benchmark]
86o_n_heap_serial value =
87 [ bgroup "elimination"
88 [
89 -- Converting the stream to an array
90 benchFold "writeLastN.Max" (S.fold (IA.writeLastN (value + 1)))
91 (P.sourceUnfoldrM value)
92 ]
93 ]
95moduleName :: String
96moduleName = "Data.Array"
98defStreamSize :: Int
99defStreamSize = defaultStreamSize
101main :: IO ()
102main = runWithCLIOpts defStreamSize allBenchmarks
104 where
106 allBenchmarks size =
107 [ bgroup (o_1_space_prefix moduleName) $
108 o_1_space_generation size ++ o_1_space_elimination size
109 , bgroup (o_n_space_prefix moduleName) $
110 o_n_heap_serial size
111 ] ++ commonBenchmarks size