on bac26fc help sign in
streamly/benchmark/LinearOps.hs
138 lines 4.5 kB history raw
1-- |
2-- Module : BenchmarkOps
3-- Copyright : (c) 2018 Harendra Kumar
4--
5-- License : MIT
6-- Maintainer : harendra.kumar@gmail.com
8module LinearOps where
10import Prelude
11 (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=),
12 subtract, undefined, Maybe, Monoid, foldMap)
14import qualified Streamly as S
15import qualified Streamly.Prelude as S
17value, appendValue, maxValue :: Int
18value = 1000000
19appendValue = 100000
20maxValue = value + 1000
22-------------------------------------------------------------------------------
23-- Benchmark ops
24-------------------------------------------------------------------------------
26{-# INLINE toNull #-}
27{-# INLINE toList #-}
28{-# INLINE foldl #-}
29{-# INLINE last #-}
30{-# INLINE scan #-}
31{-# INLINE map #-}
32{-# INLINE filterEven #-}
33{-# INLINE mapM #-}
34{-# INLINE filterAllOut #-}
35{-# INLINE filterAllIn #-}
36{-# INLINE takeOne #-}
37{-# INLINE takeAll #-}
38{-# INLINE takeWhileTrue #-}
39{-# INLINE dropAll #-}
40{-# INLINE dropWhileTrue #-}
41{-# INLINE zip #-}
42{-# INLINE concat #-}
43{-# INLINE composeMapM #-}
44{-# INLINE composeAllInFilters #-}
45{-# INLINE composeAllOutFilters #-}
46{-# INLINE composeMapAllInFilter #-}
47toNull, scan, map, filterEven, mapM, filterAllOut,
48 filterAllIn, takeOne, takeAll, takeWhileTrue, dropAll, dropWhileTrue, zip,
49 concat, composeMapM, composeAllInFilters, composeAllOutFilters,
50 composeMapAllInFilter
51 :: Monad m
52 => Stream m Int -> m ()
54toList :: Monad m => Stream m Int -> m [Int]
55foldl :: Monad m => Stream m Int -> m Int
56last :: Monad m => Stream m Int -> m (Maybe Int)
58-------------------------------------------------------------------------------
59-- Stream generation and elimination
60-------------------------------------------------------------------------------
62type Stream m a = S.StreamT m a
64source :: Int -> Stream m Int
65source n = S.fromFoldable [n..n+value]
67{-# INLINE runStream #-}
68runStream :: Monad m => Stream m a -> m ()
69runStream = S.runStream
71-------------------------------------------------------------------------------
72-- Elimination
73-------------------------------------------------------------------------------
75toNull = runStream
76toList = S.toList
77foldl = S.foldl' (+) 0
78last = S.last
80-------------------------------------------------------------------------------
81-- Transformation
82-------------------------------------------------------------------------------
84{-# INLINE transform #-}
85transform :: Monad m => Stream m a -> m ()
86transform = runStream
88scan = transform . S.scanl' (+) 0
89map = transform . fmap (+1)
90mapM = transform . S.mapM return
91filterEven = transform . S.filter even
92filterAllOut = transform . S.filter (> maxValue)
93filterAllIn = transform . S.filter (<= maxValue)
94takeOne = transform . S.take 1
95takeAll = transform . S.take maxValue
96takeWhileTrue = transform . S.takeWhile (<= maxValue)
97dropAll = transform . S.drop maxValue
98dropWhileTrue = transform . S.dropWhile (<= maxValue)
100-------------------------------------------------------------------------------
101-- Zipping and concat
102-------------------------------------------------------------------------------
104zip src = transform $ (S.zipWith (,) src src)
105concat _n = return ()
107-------------------------------------------------------------------------------
108-- Append
109-------------------------------------------------------------------------------
111{-# INLINE append #-}
112append
113 :: (Monoid (t m Int), Monad m, Monad (t m))
114 => (t m Int -> S.StreamT m Int) -> Int -> m ()
115append t n = runStream $ t $ foldMap return [n..n+appendValue]
117-------------------------------------------------------------------------------
118-- Composition
119-------------------------------------------------------------------------------
121{-# INLINE compose #-}
122compose :: Monad m => (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
123compose f = transform . f . f . f . f
125composeMapM = compose (S.mapM return)
126composeAllInFilters = compose (S.filter (<= maxValue))
127composeAllOutFilters = compose (S.filter (> maxValue))
128composeMapAllInFilter = compose (S.filter (<= maxValue) . fmap (subtract 1))
130composeScaling :: Monad m => Int -> Stream m Int -> m ()
131composeScaling m =
132 case m of
133 1 -> transform . f
134 2 -> transform . f . f
135 3 -> transform . f . f . f
136 4 -> transform . f . f . f . f
137 _ -> undefined
138 where f = S.filter (<= maxValue)