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