on 8ec2ebe help sign in
streamly/benchmark/NestedUnfoldOps.hs
128 lines 3.7 kB history raw
1-- |
2-- Module : NestedUnfoldOps
3-- Copyright : (c) 2019 Composewell Technologies
4--
5-- License : BSD3
6-- Maintainer : streamly@composewell.com
8module NestedUnfoldOps where
10import Control.Monad.IO.Class (MonadIO (..))
11import Streamly.Internal.Data.Unfold (Unfold)
13import qualified Streamly.Internal.Data.Unfold as UF
14import qualified Streamly.Internal.Data.Fold as FL
16linearCount :: Int
17linearCount = 100000
19-- n * (n + 1) / 2 == linearCount
20concatCount :: Int
21concatCount = 450
23-- double nested loop
24nestedCount2 :: Int
25nestedCount2 = round (fromIntegral linearCount**(1/2::Double))
27-- triple nested loop
28nestedCount3 :: Int
29nestedCount3 = round (fromIntegral linearCount**(1/3::Double))
31-------------------------------------------------------------------------------
32-- Stream generation and elimination
33-------------------------------------------------------------------------------
35-- generate numbers up to the argument value
36{-# INLINE source #-}
37source :: Monad m => Int -> Unfold m Int Int
38source n = UF.enumerateFromToIntegral n
40-------------------------------------------------------------------------------
41-- Benchmark ops
42-------------------------------------------------------------------------------
44{-# INLINE toNull #-}
45toNull :: MonadIO m => Int -> m ()
46toNull start = do
47 let end = start + nestedCount2
48 UF.fold
49 (UF.map (\(x, y) -> x + y)
50 $ UF.outerProduct (source end) (source end))
51 FL.drain (start, start)
53{-# INLINE toNull3 #-}
54toNull3 :: MonadIO m => Int -> m ()
55toNull3 start = do
56 let end = start + nestedCount3
57 UF.fold
58 (UF.map (\(x, y) -> x + y)
59 $ UF.outerProduct (source end)
60 ((UF.map (\(x, y) -> x + y)
61 $ UF.outerProduct (source end) (source end))))
62 FL.drain (start, (start, start))
64{-# INLINE concat #-}
65concat :: MonadIO m => Int -> m ()
66concat start = do
67 let end = start + concatCount
68 UF.fold
69 (UF.concat (source end) (source end))
70 FL.drain start
72{-# INLINE toList #-}
73toList :: MonadIO m => Int -> m [Int]
74toList start = do
75 let end = start + nestedCount2
76 UF.fold
77 (UF.map (\(x, y) -> x + y)
78 $ UF.outerProduct (source end) (source end))
79 FL.toList (start, start)
81{-# INLINE toListSome #-}
82toListSome :: MonadIO m => Int -> m [Int]
83toListSome start = do
84 let end = start + nestedCount2
85 UF.fold
86 (UF.take 1000 $ (UF.map (\(x, y) -> x + y)
87 $ UF.outerProduct (source end) (source end)))
88 FL.toList (start, start)
90{-# INLINE filterAllOut #-}
91filterAllOut :: MonadIO m => Int -> m ()
92filterAllOut start = do
93 let end = start + nestedCount2
94 UF.fold
95 (UF.filter (< 0)
96 $ UF.map (\(x, y) -> x + y)
97 $ UF.outerProduct (source end) (source end))
98 FL.drain (start, start)
100{-# INLINE filterAllIn #-}
101filterAllIn :: MonadIO m => Int -> m ()
102filterAllIn start = do
103 let end = start + nestedCount2
104 UF.fold
105 (UF.filter (> 0)
106 $ UF.map (\(x, y) -> x + y)
107 $ UF.outerProduct (source end) (source end))
108 FL.drain (start, start)
110{-# INLINE filterSome #-}
111filterSome :: MonadIO m => Int -> m ()
112filterSome start = do
113 let end = start + nestedCount2
114 UF.fold
115 (UF.filter (> 1100000)
116 $ UF.map (\(x, y) -> x + y)
117 $ UF.outerProduct (source end) (source end))
118 FL.drain (start, start)
120{-# INLINE breakAfterSome #-}
121breakAfterSome :: MonadIO m => Int -> m ()
122breakAfterSome start = do
123 let end = start + nestedCount2
124 UF.fold
125 (UF.takeWhile (<= 1100000)
126 $ UF.map (\(x, y) -> x + y)
127 $ UF.outerProduct (source end) (source end))
128 FL.drain (start, start)