@@ -0,0 +1,154 @@
2+-- Module : BenchmarkOps
3+-- Copyright : (c) 2018 Harendra Kumar
6+-- Maintainer : harendra.kumar@gmail.com
8+{-# LANGUAGE ScopedTypeVariables #-}
10+module BenchmarkNestedLoopOps where
12+import Control.Exception (try)
13+import GHC.Exception (ErrorCall)
15+import qualified Streamly as S
16+import qualified Streamly.Prelude as S
24+-------------------------------------------------------------------------------
25+-- Stream generation and elimination
26+-------------------------------------------------------------------------------
28+type Stream m a = S.SerialT m a
31+source :: S.IsStream t => Int -> Int -> t m Int
32+source start n = S.fromFoldable [start..start+n]
34+{-# INLINE runStream #-}
35+runStream :: Monad m => Stream m a -> m ()
36+runStream = S.runStream
38+{-# INLINE runToList #-}
39+runToList :: Monad m => Stream m a -> m [a]
42+-------------------------------------------------------------------------------
44+-------------------------------------------------------------------------------
46+{-# INLINE toNullLinear #-}
47+toNullLinear :: Monad m => Int -> m ()
48+toNullLinear start = runStream $ source start sumCount
50+{-# INLINE toListLinear #-}
51+toListLinear :: Monad m => Int -> m [Int]
52+toListLinear start = runToList $ source start sumCount
56+ :: (Monoid (t m Int), Monad m, Monad (t m))
57+ => (t m Int -> S.SerialT m Int) -> Int -> m ()
58+append t start = runStream $ t $ foldMap return [start..start+sumCount]
60+{-# INLINE toNull0 #-}
62+ :: (S.IsStream t, Monad m, Monad (t m))
63+ => (t m (Int, Int) -> S.SerialT m (Int, Int)) -> Int -> m ()
64+toNull0 t start = runStream . t $ do
65+ x <- source start prodCount
66+ y <- source start prodCount
69+{-# INLINE toList0 #-}
71+ :: (S.IsStream t, Monad m, Monad (t m))
72+ => (t m (Int, Int) -> S.SerialT m (Int, Int)) -> Int -> m [(Int, Int)]
73+toList0 t start = runToList . t $ do
74+ x <- source start prodCount
75+ y <- source start prodCount
80+ :: (S.IsStream t, Monad m, Monad (t m))
81+ => (t m Int -> S.SerialT m Int) -> Int -> m ()
82+toNull t start = runStream . t $ do
83+ x <- source start prodCount
84+ y <- source start prodCount
85+ return $ x * x + y * y
89+ :: (S.IsStream t, Monad m, Monad (t m))
90+ => (t m Int -> S.SerialT m Int) -> Int -> m [Int]
91+toList t start = runToList . t $ do
92+ x <- source start prodCount
93+ y <- source start prodCount
94+ return $ x * x + y * y
96+{-# INLINE toListSome #-}
98+ :: (S.IsStream t, Monad m, Monad (t m))
99+ => (t m Int -> S.SerialT m Int) -> Int -> m [Int]
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 #-}
108+ :: (S.IsStream t, Monad m, Monad (t m))
109+ => (t m Int -> S.SerialT m Int) -> Int -> m ()
110+filterAllOut t start = runStream . t $ do
111+ x <- source start prodCount
112+ y <- source start prodCount
113+ let s = x * x + y * y
118+{-# INLINE filterAllIn #-}
120+ :: (S.IsStream t, Monad m, Monad (t m))
121+ => (t m Int -> S.SerialT m Int) -> Int -> m ()
122+filterAllIn t start = runStream . t $ do
123+ x <- source start prodCount
124+ y <- source start prodCount
125+ let s = x * x + y * y
130+{-# INLINE filterSome #-}
132+ :: (S.IsStream t, Monad m, Monad (t m))
133+ => (t m Int -> S.SerialT m Int) -> Int -> m ()
134+filterSome t start = runStream . t $ do
135+ x <- source start prodCount
136+ y <- source start prodCount
137+ let s = x * x + y * y
142+{-# INLINE breakAfterSome #-}
144+ :: (S.IsStream t, Monad (t IO))
145+ => (t IO Int -> S.SerialT IO Int) -> Int -> IO ()
146+breakAfterSome 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