1{-# OPTIONS_GHC -Wno-deprecations #-}
3module Main (main) where
5import Control.DeepSeq (NFData)
6import Streamly.Data.Fold (Fold)
7import Streamly.Internal.Data.Stream (Stream)
8import System.Random (randomRIO)
10import qualified Streamly.Data.Fold as Fold
11import qualified Streamly.Internal.Data.Fold as Window
12import qualified Streamly.Internal.Data.Ring as Ring
13import qualified Streamly.Internal.Data.Stream as Stream
15import Streamly.Benchmark.Common
19source :: (Monad m, Num a, Stream.Enumerable a) =>
20 Int -> a -> Stream m a
22 Stream.enumerateFromThenTo from (from + 1) (from + fromIntegral len)
24{-# INLINE sourceDescending #-}
25sourceDescending :: (Monad m, Num a, Stream.Enumerable a) =>
26 Int -> a -> Stream m a
27sourceDescending len from =
28 Stream.enumerateFromThenTo
29 (from + fromIntegral len)
30 (from + fromIntegral (len - 1))
33{-# INLINE sourceDescendingInt #-}
34sourceDescendingInt :: Monad m => Int -> Int -> Stream m Int
35sourceDescendingInt = sourceDescending
37{-# INLINE benchWith #-}
38benchWith :: (Num a, NFData b) =>
39 (Int -> a -> Stream IO a) -> Int -> String -> Fold IO a b -> Benchmark
40benchWith src len name f =
43 $ randomRIO (1, 1 :: Int) >>= Stream.fold f . src len . fromIntegral
45{-# INLINE benchWithFold #-}
46benchWithFold :: NFData a => Int -> String -> Fold IO Double a -> Benchmark
47benchWithFold = benchWith source
49{-# INLINE benchWithFoldInt #-}
50benchWithFoldInt :: Int -> String -> Fold IO Int Int -> Benchmark
51benchWithFoldInt = benchWith source
53{-# INLINE benchScanWith #-}
54benchScanWith :: Num a =>
55 (Int -> a -> Stream IO a) -> Int -> String -> Fold IO a b -> Benchmark
56benchScanWith src len name f =
59 $ randomRIO (1, 1 :: Int)
60 >>= Stream.fold Fold.drain
65{-# INLINE benchWithPostscan #-}
66benchWithPostscan :: Int -> String -> Fold IO Double a -> Benchmark
67benchWithPostscan = benchScanWith source
69o_1_space_folds :: Int -> [Benchmark]
70o_1_space_folds numElements =
72 [ benchWithFold numElements "minimum (window size 100)"
73 (Window.windowMinimum 100)
74 , benchWithFold numElements "minimum (window size 1000)"
75 (Window.windowMinimum 1000)
76 , benchWith sourceDescendingInt numElements
77 "minimum descending (window size 1000)"
78 (Window.windowMinimum 1000)
80 , benchWithFold numElements "maximum (window size 100)"
81 (Window.windowMaximum 100)
82 , benchWithFold numElements "maximum (window size 1000)"
83 (Window.windowMaximum 1000)
84 , benchWith sourceDescendingInt numElements
85 "maximum descending (window size 1000)"
86 (Window.windowMaximum 1000)
88 , benchWithFold numElements "range (window size 100)"
89 (Window.windowRange 100)
90 , benchWithFold numElements "range (window size 1000)"
91 (Window.windowRange 1000)
92 , benchWith sourceDescendingInt numElements
93 "range descending (window size 1000)"
94 (Window.windowRange 1000)
96 , benchWithFoldInt numElements "sumInt (window size 100)"
97 (Ring.slidingWindow 100 Window.windowSumInt)
98 , benchWithFoldInt numElements "sum for Int (window size 100)"
99 (Ring.slidingWindow 100 Window.windowSum)
100 , benchWithFold numElements "sum (window size 100)"
101 (Ring.slidingWindow 100 Window.windowSum)
102 , benchWithFold numElements "sum (window size 1000)"
103 (Ring.slidingWindow 1000 Window.windowSum)
104 , benchWithFold numElements "sum (entire stream)"
105 (Window.cumulative Window.windowSum)
106 , benchWithFold numElements "sum (Data.Fold)"
109 , benchWithFold numElements "mean (window size 100)"
110 (Ring.slidingWindow 100 Window.windowMean)
111 , benchWithFold numElements "mean (window size 1000)"
112 (Ring.slidingWindow 1000 Window.windowMean)
113 , benchWithFold numElements "mean (entire stream)"
114 (Window.cumulative Window.windowMean)
115 , benchWithFold numElements "mean (Data.Fold)"
118 , benchWithFold numElements "powerSum 2 (window size 100)"
119 (Ring.slidingWindow 100 (Window.windowPowerSum 2))
120 , benchWithFold numElements "powerSum 2 (entire stream)"
121 (Window.cumulative (Window.windowPowerSum 2))
126o_1_space_scans :: Int -> [Benchmark]
127o_1_space_scans numElements =
129 [ benchWithPostscan numElements "minimum (window size 10)"
130 (Window.windowMinimum 10)
131 -- Below window size 30 the linear search based impl performs better
132 -- than the dequeue based implementation.
133 , benchWithPostscan numElements "minimum (window size 30)"
134 (Window.windowMinimum 30)
135 , benchWithPostscan numElements "minimum (window size 1000)"
136 (Window.windowMinimum 1000)
137 , benchScanWith sourceDescendingInt numElements
138 "minimum descending (window size 1000)"
139 (Window.windowMinimum 1000)
141 , benchWithPostscan numElements "maximum (window size 10)"
142 (Window.windowMaximum 10)
143 , benchWithPostscan numElements "maximum (window size 30)"
144 (Window.windowMaximum 30)
145 , benchWithPostscan numElements "maximum (window size 1000)"
146 (Window.windowMaximum 1000)
147 , benchScanWith sourceDescendingInt numElements
148 "maximum descending (window size 1000)"
149 (Window.windowMaximum 1000)
151 , benchWithPostscan numElements "range (window size 10)"
152 (Window.windowRange 10)
153 , benchWithPostscan numElements "range (window size 30)"
154 (Window.windowRange 30)
155 , benchWithPostscan numElements "range (window size 1000)"
156 (Window.windowRange 1000)
157 , benchScanWith sourceDescendingInt numElements
158 "range descending (window size 1000)"
159 (Window.windowRange 1000)
161 , benchWithPostscan numElements "sum (window size 100)"
162 (Ring.slidingWindow 100 Window.windowSum)
163 , benchWithPostscan numElements "sum (window size 1000)"
164 (Ring.slidingWindow 1000 Window.windowSum)
166 , benchWithPostscan numElements "mean (window size 100)"
167 (Ring.slidingWindow 100 Window.windowMean)
168 , benchWithPostscan numElements "mean (window size 1000)"
169 (Ring.slidingWindow 1000 Window.windowMean)
171 , benchWithPostscan numElements "powerSum 2 (window size 100)"
172 (Ring.slidingWindow 100 (Window.windowPowerSum 2))
173 , benchWithPostscan numElements "powerSum 2 (window size 1000)"
174 (Ring.slidingWindow 1000 (Window.windowPowerSum 2))
179moduleName = "Data.Fold.Window"
182main = runWithCLIOpts defaultStreamSize allBenchmarks
186 allBenchmarks value =
187 [ bgroup (o_1_space_prefix moduleName) $ concat
188 [ o_1_space_folds value
189 , o_1_space_scans value
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list