on d59ff9d help sign in
streamly/benchmark/NestedOps.hs
154 lines 4.3 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.StreamT m a
30{-# INLINE source #-}
31source :: S.IsStream t => Int -> Int -> t m Int
32source start n = S.fromFoldable [start..start+n]
34{-# INLINE runStream #-}
35runStream :: Monad m => Stream m a -> m ()
36runStream = S.runStream
38{-# INLINE runToList #-}
39runToList :: Monad m => Stream m a -> m [a]
40runToList = S.toList
42-------------------------------------------------------------------------------
43-- Benchmark ops
44-------------------------------------------------------------------------------
46{-# INLINE toNullLinear #-}
47toNullLinear :: Monad m => Int -> m ()
48toNullLinear start = runStream $ source start sumCount
50{-# INLINE toListLinear #-}
51toListLinear :: Monad m => Int -> m [Int]
52toListLinear start = runToList $ source start sumCount
54{-# INLINE append #-}
55append
56 :: (Monoid (t m Int), Monad m, Monad (t m))
57 => (t m Int -> S.StreamT m Int) -> Int -> m ()
58append t start = runStream $ t $ foldMap return [start..start+sumCount]
60{-# INLINE toNull0 #-}
61toNull0
62 :: (S.IsStream t, Monad m, Monad (t m))
63 => (t m (Int, Int) -> S.StreamT m (Int, Int)) -> Int -> m ()
64toNull0 t start = runStream . t $ do
65 x <- source start prodCount
66 y <- source start prodCount
67 return (x,y)
69{-# INLINE toList0 #-}
70toList0
71 :: (S.IsStream t, Monad m, Monad (t m))
72 => (t m (Int, Int) -> S.StreamT m (Int, Int)) -> Int -> m [(Int, Int)]
73toList0 t start = runToList . t $ do
74 x <- source start prodCount
75 y <- source start prodCount
76 return (x,y)
78{-# INLINE toNull #-}
79toNull
80 :: (S.IsStream t, Monad m, Monad (t m))
81 => (t m Int -> S.StreamT m Int) -> Int -> m ()
82toNull t start = runStream . t $ do
83 x <- source start prodCount
84 y <- source start prodCount
85 return $ x * x + y * y
87{-# INLINE toList #-}
88toList
89 :: (S.IsStream t, Monad m, Monad (t m))
90 => (t m Int -> S.StreamT m Int) -> Int -> m [Int]
91toList t start = runToList . t $ do
92 x <- source start prodCount
93 y <- source start prodCount
94 return $ x * x + y * y
96{-# INLINE toListSome #-}
97toListSome
98 :: (S.IsStream t, Monad m, Monad (t m))
99 => (t m Int -> S.StreamT m Int) -> Int -> m [Int]
100toListSome t start =
101 runToList . t $ S.take 1000 $ do
102 x <- source start prodCount
103 y <- source start prodCount
104 return $ x * x + y * y
106{-# INLINE filterAllOut #-}
107filterAllOut
108 :: (S.IsStream t, Monad m, Monad (t m))
109 => (t m Int -> S.StreamT m Int) -> Int -> m ()
110filterAllOut t start = runStream . t $ do
111 x <- source start prodCount
112 y <- source start prodCount
113 let s = x * x + y * y
114 if (s < 1)
115 then return s
116 else S.nil
118{-# INLINE filterAllIn #-}
119filterAllIn
120 :: (S.IsStream t, Monad m, Monad (t m))
121 => (t m Int -> S.StreamT m Int) -> Int -> m ()
122filterAllIn t start = runStream . t $ do
123 x <- source start prodCount
124 y <- source start prodCount
125 let s = x * x + y * y
126 if (s > 1)
127 then return s
128 else S.nil
130{-# INLINE filterSome #-}
131filterSome
132 :: (S.IsStream t, Monad m, Monad (t m))
133 => (t m Int -> S.StreamT m Int) -> Int -> m ()
134filterSome t start = runStream . t $ do
135 x <- source start prodCount
136 y <- source start prodCount
137 let s = x * x + y * y
138 if (s > 1100000)
139 then return s
140 else S.nil
142{-# INLINE breakAfterSome #-}
143breakAfterSome
144 :: (S.IsStream t, Monad (t IO))
145 => (t IO Int -> S.StreamT IO Int) -> Int -> IO ()
146breakAfterSome t start = do
147 (_ :: Either ErrorCall ()) <- try $ runStream . t $ do
148 x <- source start prodCount
149 y <- source start prodCount
150 let s = x * x + y * y
151 if (s > 1100000)
152 then error "break"
153 else return s
154 return ()