on c3a5ef2 help sign in
streamly/benchmark/NestedOps.hs
159 lines 4.4 kB history raw
1-- |
2-- Module : BenchmarkOps
3-- Copyright : (c) 2018 Harendra Kumar
4--
5-- License : MIT
6-- Maintainer : harendra.kumar@gmail.com
8{-# LANGUAGE ScopedTypeVariables #-}
10module NestedOps where
12import Control.Exception (try)
13import GHC.Exception (ErrorCall)
15import qualified Streamly as S
16import qualified Streamly.Prelude as S
18sumCount :: Int
19sumCount = 1000000
21prodCount :: Int
22prodCount = 1000
24-------------------------------------------------------------------------------
25-- Stream generation and elimination
26-------------------------------------------------------------------------------
28type Stream m a = S.SerialT m a
30{-# INLINE source #-}
31source :: S.IsStream t => Int -> Int -> t m Int
32source start n = S.unfoldr step start
33 where
34 step cnt =
35 if cnt > start + n
36 then Nothing
37 else (Just (cnt, cnt + 1))
39{-# INLINE runStream #-}
40runStream :: Monad m => Stream m a -> m ()
41runStream = S.runStream
43{-# INLINE runToList #-}
44runToList :: Monad m => Stream m a -> m [a]
45runToList = S.toList
47-------------------------------------------------------------------------------
48-- Benchmark ops
49-------------------------------------------------------------------------------
51{-# INLINE toNullLinear #-}
52toNullLinear :: Monad m => Int -> m ()
53toNullLinear start = runStream $ source start sumCount
55{-# INLINE toListLinear #-}
56toListLinear :: Monad m => Int -> m [Int]
57toListLinear start = runToList $ source start sumCount
59{-# INLINE append #-}
60append
61 :: (Monoid (t m Int), Monad m, Monad (t m))
62 => (t m Int -> S.SerialT m Int) -> Int -> m ()
63append t start = runStream $ t $ foldMap return [start..start+sumCount]
65{-# INLINE toNull0 #-}
66toNull0
67 :: (S.IsStream t, Monad m, Monad (t m))
68 => (t m (Int, Int) -> S.SerialT m (Int, Int)) -> Int -> m ()
69toNull0 t start = runStream . t $ do
70 x <- source start prodCount
71 y <- source start prodCount
72 return (x,y)
74{-# INLINE toList0 #-}
75toList0
76 :: (S.IsStream t, Monad m, Monad (t m))
77 => (t m (Int, Int) -> S.SerialT m (Int, Int)) -> Int -> m [(Int, Int)]
78toList0 t start = runToList . t $ do
79 x <- source start prodCount
80 y <- source start prodCount
81 return (x,y)
83{-# INLINE toNull #-}
84toNull
85 :: (S.IsStream t, Monad m, Monad (t m))
86 => (t m Int -> S.SerialT m Int) -> Int -> m ()
87toNull t start = runStream . t $ do
88 x <- source start prodCount
89 y <- source start prodCount
90 return $ x * x + y * y
92{-# INLINE toList #-}
93toList
94 :: (S.IsStream t, Monad m, Monad (t m))
95 => (t m Int -> S.SerialT m Int) -> Int -> m [Int]
96toList t start = runToList . t $ do
97 x <- source start prodCount
98 y <- source start prodCount
99 return $ x * x + y * y
101{-# INLINE toListSome #-}
102toListSome
103 :: (S.IsStream t, Monad m, Monad (t m))
104 => (t m Int -> S.SerialT m Int) -> Int -> m [Int]
105toListSome t start =
106 runToList . t $ S.take 1000 $ do
107 x <- source start prodCount
108 y <- source start prodCount
109 return $ x * x + y * y
111{-# INLINE filterAllOut #-}
112filterAllOut
113 :: (S.IsStream t, Monad m, Monad (t m))
114 => (t m Int -> S.SerialT m Int) -> Int -> m ()
115filterAllOut t start = runStream . t $ do
116 x <- source start prodCount
117 y <- source start prodCount
118 let s = x * x + y * y
119 if (s < 1)
120 then return s
121 else S.nil
123{-# INLINE filterAllIn #-}
124filterAllIn
125 :: (S.IsStream t, Monad m, Monad (t m))
126 => (t m Int -> S.SerialT m Int) -> Int -> m ()
127filterAllIn t start = runStream . t $ do
128 x <- source start prodCount
129 y <- source start prodCount
130 let s = x * x + y * y
131 if (s > 1)
132 then return s
133 else S.nil
135{-# INLINE filterSome #-}
136filterSome
137 :: (S.IsStream t, Monad m, Monad (t m))
138 => (t m Int -> S.SerialT m Int) -> Int -> m ()
139filterSome t start = runStream . t $ do
140 x <- source start prodCount
141 y <- source start prodCount
142 let s = x * x + y * y
143 if (s > 1100000)
144 then return s
145 else S.nil
147{-# INLINE breakAfterSome #-}
148breakAfterSome
149 :: (S.IsStream t, Monad (t IO))
150 => (t IO Int -> S.SerialT IO Int) -> Int -> IO ()
151breakAfterSome t start = do
152 (_ :: Either ErrorCall ()) <- try $ runStream . t $ do
153 x <- source start prodCount
154 y <- source start prodCount
155 let s = x * x + y * y
156 if (s > 1100000)
157 then error "break"
158 else return s
159 return ()