on master help sign in
pass stream-size from CLI for nested-unfold
eedd741Harendra Kumar
benchmark/NestedUnfold.hs+17−11
@@ -8,6 +8,8 @@
8import Control.DeepSeq (NFData)
8import Control.DeepSeq (NFData)
9import System.Random (randomRIO)
9import System.Random (randomRIO)
10
10
11+import Common (parseCLIOpts)
12+
11import qualified NestedUnfoldOps as Ops
13import qualified NestedUnfoldOps as Ops
12
14
13import Gauge
15import Gauge
@@ -15,18 +17,22 @@ import Gauge
15benchIO :: (NFData b) => String -> (Int -> IO b) -> Benchmark
17benchIO :: (NFData b) => String -> (Int -> IO b) -> Benchmark
16benchIO name f = bench name $ nfIO $ randomRIO (1,1) >>= f
18benchIO name f = bench name $ nfIO $ randomRIO (1,1) >>= f
17
19
20+defaultStreamSize :: Int
21+defaultStreamSize = 100000
22+
18main :: IO ()
23main :: IO ()
19−main =
24+main = do
20− defaultMain
25+ (linearCount, cfg, benches) <- parseCLIOpts defaultStreamSize
26+ linearCount `seq` runMode (mode cfg) cfg benches
21 [ bgroup "unfold"
27 [ bgroup "unfold"
22− [ benchIO "toNull" $ Ops.toNull
28+ [ benchIO "toNull" $ Ops.toNull linearCount
23− , benchIO "toNull3" $ Ops.toNull3
29+ , benchIO "toNull3" $ Ops.toNull3 linearCount
24− , benchIO "concat" $ Ops.concat
30+ , benchIO "concat" $ Ops.concat linearCount
25− , benchIO "toList" $ Ops.toList
31+ -- , benchIO "toList" $ Ops.toList
26− , benchIO "toListSome" $ Ops.toListSome
32+ , benchIO "toListSome" $ Ops.toListSome linearCount
27− , benchIO "filterAllOut" $ Ops.filterAllOut
33+ , benchIO "filterAllOut" $ Ops.filterAllOut linearCount
28− , benchIO "filterAllIn" $ Ops.filterAllIn
34+ , benchIO "filterAllIn" $ Ops.filterAllIn linearCount
29− , benchIO "filterSome" $ Ops.filterSome
35+ , benchIO "filterSome" $ Ops.filterSome linearCount
30− , benchIO "breakAfterSome" $ Ops.breakAfterSome
36+ , benchIO "breakAfterSome" $ Ops.breakAfterSome linearCount
31 ]
37 ]
32 ]
38 ]
benchmark/NestedUnfoldOps.hs+34−36
@@ -13,20 +13,18 @@ import Streamly.Internal.Data.Unfold (Unfold)
13import qualified Streamly.Internal.Data.Unfold as UF
13import qualified Streamly.Internal.Data.Unfold as UF
14import qualified Streamly.Internal.Data.Fold as FL
14import qualified Streamly.Internal.Data.Fold as FL
15
15
16−linearCount :: Int
17−linearCount = 100000
18−
19-- n * (n + 1) / 2 == linearCount
16-- n * (n + 1) / 2 == linearCount
20−concatCount :: Int
17+concatCount :: Int -> Int
21−concatCount = 450
18+concatCount linearCount =
19+ round (((1 + 8 * fromIntegral linearCount)**(1/2::Double) - 1) / 2)
22
20
23-- double nested loop
21-- double nested loop
24−nestedCount2 :: Int
22+nestedCount2 :: Int -> Int
25−nestedCount2 = round (fromIntegral linearCount**(1/2::Double))
23+nestedCount2 linearCount = round (fromIntegral linearCount**(1/2::Double))
26
24
27-- triple nested loop
25-- triple nested loop
28−nestedCount3 :: Int
26+nestedCount3 :: Int -> Int
29−nestedCount3 = round (fromIntegral linearCount**(1/3::Double))
27+nestedCount3 linearCount = round (fromIntegral linearCount**(1/3::Double))
30
28
31-------------------------------------------------------------------------------
29-------------------------------------------------------------------------------
32-- Stream generation and elimination
30-- Stream generation and elimination
@@ -42,18 +40,18 @@ source n = UF.enumerateFromToIntegral n
42-------------------------------------------------------------------------------
40-------------------------------------------------------------------------------
43
41
44{-# INLINE toNull #-}
42{-# INLINE toNull #-}
45−toNull :: MonadIO m => Int -> m ()
43+toNull :: MonadIO m => Int -> Int -> m ()
46−toNull start = do
44+toNull linearCount start = do
47− let end = start + nestedCount2
45+ let end = start + nestedCount2 linearCount
48 UF.fold
46 UF.fold
49 (UF.map (\(x, y) -> x + y)
47 (UF.map (\(x, y) -> x + y)
50 $ UF.outerProduct (source end) (source end))
48 $ UF.outerProduct (source end) (source end))
51 FL.drain (start, start)
49 FL.drain (start, start)
52
50
53{-# INLINE toNull3 #-}
51{-# INLINE toNull3 #-}
54−toNull3 :: MonadIO m => Int -> m ()
52+toNull3 :: MonadIO m => Int -> Int -> m ()
55−toNull3 start = do
53+toNull3 linearCount start = do
56− let end = start + nestedCount3
54+ let end = start + nestedCount3 linearCount
57 UF.fold
55 UF.fold
58 (UF.map (\(x, y) -> x + y)
56 (UF.map (\(x, y) -> x + y)
59 $ UF.outerProduct (source end)
57 $ UF.outerProduct (source end)
@@ -62,35 +60,35 @@ toNull3 start = do
62 FL.drain (start, (start, start))
60 FL.drain (start, (start, start))
63
61
64{-# INLINE concat #-}
62{-# INLINE concat #-}
65−concat :: MonadIO m => Int -> m ()
63+concat :: MonadIO m => Int -> Int -> m ()
66−concat start = do
64+concat linearCount start = do
67− let end = start + concatCount
65+ let end = start + concatCount linearCount
68 UF.fold
66 UF.fold
69 (UF.concat (source end) (source end))
67 (UF.concat (source end) (source end))
70 FL.drain start
68 FL.drain start
71
69
72{-# INLINE toList #-}
70{-# INLINE toList #-}
73−toList :: MonadIO m => Int -> m [Int]
71+toList :: MonadIO m => Int -> Int -> m [Int]
74−toList start = do
72+toList linearCount start = do
75− let end = start + nestedCount2
73+ let end = start + nestedCount2 linearCount
76 UF.fold
74 UF.fold
77 (UF.map (\(x, y) -> x + y)
75 (UF.map (\(x, y) -> x + y)
78 $ UF.outerProduct (source end) (source end))
76 $ UF.outerProduct (source end) (source end))
79 FL.toList (start, start)
77 FL.toList (start, start)
80
78
81{-# INLINE toListSome #-}
79{-# INLINE toListSome #-}
82−toListSome :: MonadIO m => Int -> m [Int]
80+toListSome :: MonadIO m => Int -> Int -> m [Int]
83−toListSome start = do
81+toListSome linearCount start = do
84− let end = start + nestedCount2
82+ let end = start + nestedCount2 linearCount
85 UF.fold
83 UF.fold
86 (UF.take 1000 $ (UF.map (\(x, y) -> x + y)
84 (UF.take 1000 $ (UF.map (\(x, y) -> x + y)
87 $ UF.outerProduct (source end) (source end)))
85 $ UF.outerProduct (source end) (source end)))
88 FL.toList (start, start)
86 FL.toList (start, start)
89
87
90{-# INLINE filterAllOut #-}
88{-# INLINE filterAllOut #-}
91−filterAllOut :: MonadIO m => Int -> m ()
89+filterAllOut :: MonadIO m => Int -> Int -> m ()
92−filterAllOut start = do
90+filterAllOut linearCount start = do
93− let end = start + nestedCount2
91+ let end = start + nestedCount2 linearCount
94 UF.fold
92 UF.fold
95 (UF.filter (< 0)
93 (UF.filter (< 0)
96 $ UF.map (\(x, y) -> x + y)
94 $ UF.map (\(x, y) -> x + y)
@@ -98,9 +96,9 @@ filterAllOut start = do
98 FL.drain (start, start)
96 FL.drain (start, start)
99
97
100{-# INLINE filterAllIn #-}
98{-# INLINE filterAllIn #-}
101−filterAllIn :: MonadIO m => Int -> m ()
99+filterAllIn :: MonadIO m => Int -> Int -> m ()
102−filterAllIn start = do
100+filterAllIn linearCount start = do
103− let end = start + nestedCount2
101+ let end = start + nestedCount2 linearCount
104 UF.fold
102 UF.fold
105 (UF.filter (> 0)
103 (UF.filter (> 0)
106 $ UF.map (\(x, y) -> x + y)
104 $ UF.map (\(x, y) -> x + y)
@@ -108,9 +106,9 @@ filterAllIn start = do
108 FL.drain (start, start)
106 FL.drain (start, start)
109
107
110{-# INLINE filterSome #-}
108{-# INLINE filterSome #-}
111−filterSome :: MonadIO m => Int -> m ()
109+filterSome :: MonadIO m => Int -> Int -> m ()
112−filterSome start = do
110+filterSome linearCount start = do
113− let end = start + nestedCount2
111+ let end = start + nestedCount2 linearCount
114 UF.fold
112 UF.fold
115 (UF.filter (> 1100000)
113 (UF.filter (> 1100000)
116 $ UF.map (\(x, y) -> x + y)
114 $ UF.map (\(x, y) -> x + y)
@@ -118,9 +116,9 @@ filterSome start = do
118 FL.drain (start, start)
116 FL.drain (start, start)
119
117
120{-# INLINE breakAfterSome #-}
118{-# INLINE breakAfterSome #-}
121−breakAfterSome :: MonadIO m => Int -> m ()
119+breakAfterSome :: MonadIO m => Int -> Int -> m ()
122−breakAfterSome start = do
120+breakAfterSome linearCount start = do
123− let end = start + nestedCount2
121+ let end = start + nestedCount2 linearCount
124 UF.fold
122 UF.fold
125 (UF.takeWhile (<= 1100000)
123 (UF.takeWhile (<= 1100000)
126 $ UF.map (\(x, y) -> x + y)
124 $ UF.map (\(x, y) -> x + y)