2-- Module : BenchmarkOps
3-- Copyright : (c) 2018 Harendra Kumar
6-- Maintainer : harendra.kumar@gmail.com
9{-# LANGUAGE FlexibleContexts #-}
10{-# LANGUAGE ScopedTypeVariables #-}
14import Control.Monad (when)
15import Data.Functor.Identity (Identity, runIdentity)
16import Data.Maybe (fromJust)
18 (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=), (==), (>=),
19 subtract, undefined, Maybe(..), odd, Bool, not, (>>=), mapM_, curry,
20 maxBound, div, IO, compare)
21import qualified Prelude as P
22import qualified Data.Foldable as F
23import qualified GHC.Exts as GHC
25import qualified Streamly as S
26import qualified Streamly.Prelude as S
36-------------------------------------------------------------------------------
38-------------------------------------------------------------------------------
40-------------------------------------------------------------------------------
41-- Stream generation and elimination
42-------------------------------------------------------------------------------
44type Stream m a = S.SerialT m a
47source :: (S.MonadAsync m, S.IsStream t) => Int -> t m Int
48source n = S.serially $ sourceUnfoldrM n
49-- source n = S.serially $ sourceFromList n
51{-# INLINE sourceFromList #-}
52sourceFromList :: (Monad m, S.IsStream t) => Int -> t m Int
53sourceFromList n = S.fromList [n..n+value]
55{-# INLINE sourceFromListM #-}
56sourceFromListM :: (S.MonadAsync m, S.IsStream t) => Int -> t m Int
57sourceFromListM n = S.fromListM (Prelude.fmap return [n..n+value])
59{-# INLINE sourceFromFoldable #-}
60sourceFromFoldable :: S.IsStream t => Int -> t m Int
61sourceFromFoldable n = S.fromFoldable [n..n+value]
63{-# INLINE sourceFromFoldableM #-}
64sourceFromFoldableM :: (S.IsStream t, S.MonadAsync m) => Int -> t m Int
65sourceFromFoldableM n = S.fromFoldableM (Prelude.fmap return [n..n+value])
67{-# INLINE sourceFoldMapWith #-}
68sourceFoldMapWith :: (S.IsStream t, S.Semigroup (t m Int))
70sourceFoldMapWith n = S.foldMapWith (S.<>) S.yield [n..n+value]
72{-# INLINE sourceFoldMapWithM #-}
73sourceFoldMapWithM :: (S.IsStream t, Monad m, S.Semigroup (t m Int))
75sourceFoldMapWithM n = S.foldMapWith (S.<>) (S.yieldM . return) [n..n+value]
77{-# INLINE sourceUnfoldr #-}
78sourceUnfoldr :: (Monad m, S.IsStream t) => Int -> t m Int
79sourceUnfoldr n = S.unfoldr step n
84 else Just (cnt, cnt + 1)
86{-# INLINE sourceUnfoldrM #-}
87sourceUnfoldrM :: (S.IsStream t, S.MonadAsync m) => Int -> t m Int
88sourceUnfoldrM n = S.unfoldrM step n
93 else return (Just (cnt, cnt + 1))
95{-# INLINE sourceUnfoldrMN #-}
96sourceUnfoldrMN :: (S.IsStream t, S.MonadAsync m) => Int -> Int -> t m Int
97sourceUnfoldrMN m n = S.unfoldrM step n
102 else return (Just (cnt, cnt + 1))
104{-# INLINE sourceUnfoldrMAction #-}
105sourceUnfoldrMAction :: (S.IsStream t, S.MonadAsync m) => Int -> t m (m Int)
106sourceUnfoldrMAction n = S.serially $ S.unfoldrM step n
111 else return (Just (return cnt, cnt + 1))
113-------------------------------------------------------------------------------
114-- Pure stream generation
115-------------------------------------------------------------------------------
117{-# INLINE sourceIsList #-}
118sourceIsList :: Int -> S.SerialT Identity Int
119sourceIsList n = GHC.fromList [n..n+value]
121{-# INLINE sourceIsString #-}
122sourceIsString :: Int -> S.SerialT Identity P.Char
123sourceIsString n = GHC.fromString (P.replicate (n + value) 'a')
125-------------------------------------------------------------------------------
127-------------------------------------------------------------------------------
129{-# INLINE runStream #-}
130runStream :: Monad m => Stream m a -> m ()
131runStream = S.runStream
136toList, foldr, foldrM :: Monad m => Stream m Int -> m [Int]
139{-# INLINE maximum #-}
140{-# INLINE minimum #-}
142{-# INLINE findIndex #-}
143{-# INLINE elemIndex #-}
144{-# INLINE foldl1' #-}
146last, minimum, maximum, find, findIndex, elemIndex, foldl1', foldr1
147 :: Monad m => Stream m Int -> m (Maybe Int)
149{-# INLINE minimumBy #-}
150{-# INLINE maximumBy #-}
151minimumBy, maximumBy :: Monad m => Stream m Int -> m (Maybe Int)
156{-# INLINE product #-}
157foldl', length, sum, product :: Monad m => Stream m Int -> m Int
164{-# INLINE notElem #-}
165elem, notElem, all, any, and, or :: Monad m => Stream m Int -> m Bool
168toNull :: Monad m => (t m Int -> S.SerialT m Int) -> t m Int -> m ()
169toNull t = runStream . t
172uncons :: Monad m => Stream m Int -> m ()
177 Just (_, t) -> uncons t
180init :: Monad m => Stream m a -> m ()
181init s = S.init s >>= Prelude.mapM_ S.runStream
184tail :: Monad m => Stream m a -> m ()
185tail s = S.tail s >>= Prelude.mapM_ tail
187{-# INLINE nullHeadTail #-}
188nullHeadTail :: Monad m => Stream m Int -> m ()
193 S.tail s >>= Prelude.mapM_ nullHeadTail
196mapM_ :: Monad m => Stream m Int -> m ()
197mapM_ = S.mapM_ (\_ -> return ())
200foldr = S.foldr (:) []
202foldrM = S.foldrM (\a xs -> return (a : xs)) []
203foldl' = S.foldl' (+) 0
204foldl1' = S.foldl1' (+)
206elem = S.elem maxValue
207notElem = S.notElem maxValue
209all = S.all (<= maxValue)
210any = S.any (> maxValue)
211and = S.and . S.map (<= maxValue)
212or = S.or . S.map (> maxValue)
213find = S.find (== maxValue)
214findIndex = S.findIndex (== maxValue)
215elemIndex = S.elemIndex maxValue
220minimumBy = S.minimumBy compare
221maximumBy = S.maximumBy compare
223-------------------------------------------------------------------------------
225-------------------------------------------------------------------------------
227{-# INLINE transform #-}
228transform :: Monad m => Stream m a -> m ()
231{-# INLINE composeN #-}
234 => Int -> (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
238 2 -> transform . f . f
239 3 -> transform . f . f . f
240 4 -> transform . f . f . f . f
243-- polymorphic stream version of composeN
244{-# INLINE composeN' #-}
246 :: (S.IsStream t, Monad m)
247 => Int -> (t m Int -> Stream m Int) -> t m Int -> m ()
251 2 -> transform . f . S.adapt . f
252 3 -> transform . f . S.adapt . f . S.adapt . f
253 4 -> transform . f . S.adapt . f . S.adapt . f . S.adapt . f
260{-# INLINE mapMaybe #-}
261{-# INLINE filterEven #-}
262{-# INLINE filterAllOut #-}
263{-# INLINE filterAllIn #-}
264{-# INLINE takeOne #-}
265{-# INLINE takeAll #-}
266{-# INLINE takeWhileTrue #-}
267{-# INLINE takeWhileMTrue #-}
268{-# INLINE dropOne #-}
269{-# INLINE dropAll #-}
270{-# INLINE dropWhileTrue #-}
271{-# INLINE dropWhileMTrue #-}
272{-# INLINE dropWhileFalse #-}
273{-# INLINE findIndices #-}
274{-# INLINE elemIndices #-}
275{-# INLINE insertLarge #-}
276{-# INLINE insertSmall #-}
277{-# INLINE deleteLarge #-}
278{-# INLINE deleteSmall #-}
279scan, scanl1, map, fmap, mapMaybe, filterEven, filterAllOut,
280 filterAllIn, takeOne, takeAll, takeWhileTrue, takeWhileMTrue, dropOne,
281 dropAll, dropWhileTrue, dropWhileMTrue, dropWhileFalse,
282 findIndices, elemIndices, insertLarge, insertSmall, deleteLarge,
285 => Int -> Stream m Int -> m ()
287{-# INLINE mapMaybeM #-}
288mapMaybeM :: S.MonadAsync m => Int -> Stream m Int -> m ()
291mapM :: (S.IsStream t, S.MonadAsync m)
292 => (t m Int -> S.SerialT m Int) -> Int -> t m Int -> m ()
294{-# INLINE sequence #-}
295sequence :: (S.IsStream t, S.MonadAsync m)
296 => (t m Int -> S.SerialT m Int) -> t m (m Int) -> m ()
298scan n = composeN n $ S.scanl' (+) 0
299scanl1 n = composeN n $ S.scanl1' (+)
300fmap n = composeN n $ Prelude.fmap (+1)
301map n = composeN n $ S.map (+1)
302mapM t n = composeN' n $ t . S.mapM return
303mapMaybe n = composeN n $ S.mapMaybe
304 (\x -> if Prelude.odd x then Nothing else Just x)
305mapMaybeM n = composeN n $ S.mapMaybeM
306 (\x -> if Prelude.odd x then return Nothing else return $ Just x)
307sequence t = transform . t . S.sequence
308filterEven n = composeN n $ S.filter even
309filterAllOut n = composeN n $ S.filter (> maxValue)
310filterAllIn n = composeN n $ S.filter (<= maxValue)
311takeOne n = composeN n $ S.take 1
312takeAll n = composeN n $ S.take maxValue
313takeWhileTrue n = composeN n $ S.takeWhile (<= maxValue)
314takeWhileMTrue n = composeN n $ S.takeWhileM (return . (<= maxValue))
315dropOne n = composeN n $ S.drop 1
316dropAll n = composeN n $ S.drop maxValue
317dropWhileTrue n = composeN n $ S.dropWhile (<= maxValue)
318dropWhileMTrue n = composeN n $ S.dropWhileM (return . (<= maxValue))
319dropWhileFalse n = composeN n $ S.dropWhile (> maxValue)
320findIndices n = composeN n $ S.findIndices (== maxValue)
321elemIndices n = composeN n $ S.elemIndices maxValue
322insertLarge n = composeN n $ S.insertBy compare maxValue
323insertSmall n = composeN n $ S.insertBy compare 0
324deleteSmall n = composeN n $ S.deleteBy (==) 0
325deleteLarge n = composeN n $ S.deleteBy (>=) maxValue
328-------------------------------------------------------------------------------
330-------------------------------------------------------------------------------
332iterStreamLen, maxIters :: Int
336{-# INLINE iterateSource #-}
339 => (Stream m Int -> Stream m Int) -> Int -> Int -> Stream m Int
340iterateSource g i n = f i (sourceUnfoldrMN iterStreamLen n)
343 f x m = g (f (x P.- 1) m)
345{-# INLINE iterateMapM #-}
346{-# INLINE iterateScan #-}
347{-# INLINE iterateScanl1 #-}
348{-# INLINE iterateFilterEven #-}
349{-# INLINE iterateTakeAll #-}
350{-# INLINE iterateDropOne #-}
351{-# INLINE iterateDropWhileFalse #-}
352{-# INLINE iterateDropWhileTrue #-}
353iterateMapM, iterateScan, iterateScanl1, iterateFilterEven, iterateTakeAll,
354 iterateDropOne, iterateDropWhileFalse, iterateDropWhileTrue
356 => Int -> Stream m Int
359iterateScan = iterateSource (S.scanl' (+) 0) (maxIters `div` 10)
361iterateScanl1 = iterateSource (S.scanl1' (+)) (maxIters `div` 10)
363iterateMapM = iterateSource (S.mapM return) maxIters
364iterateFilterEven = iterateSource (S.filter even) maxIters
365iterateTakeAll = iterateSource (S.take maxValue) maxIters
366iterateDropOne = iterateSource (S.drop 1) maxIters
367iterateDropWhileFalse = iterateSource (S.dropWhile (> maxValue)) maxIters
368iterateDropWhileTrue = iterateSource (S.dropWhile (<= maxValue)) maxIters
370-------------------------------------------------------------------------------
372-------------------------------------------------------------------------------
378{-# INLINE isPrefixOf #-}
379{-# INLINE isPrefixAll #-}
380{-# INLINE isSubsequenceOf #-}
381{-# INLINE evenSubsequence #-}
382{-# INLINE stripPrefix #-}
383zip, zipM, concat, merge, isPrefixOf, isPrefixAll, isSubsequenceOf,
384 evenSubsequence, stripPrefix
385 :: Monad m => Stream m Int -> m ()
389 let src1 = fromJust r
390 transform (S.zipWith (,) src src1)
393 let src1 = fromJust r
394 transform (S.zipWithM (curry return) src src1)
398 let src1 = fromJust r
399 transform (S.mergeBy P.compare src src1)
402 let src1 = S.take 10 src
403 _ <- S.isPrefixOf src1 src
407 _ <- S.isPrefixOf src src
410isSubsequenceOf src = do
411 let src1 = S.take 10 src
412 _ <- S.isSubsequenceOf src1 src
415evenSubsequence src = do
416 let src1 = S.filter even src
417 _ <- S.isSubsequenceOf src1 src
421 let src1 = S.take 10 src
422 _ <- S.stripPrefix src1 src
425{-# INLINE zipAsync #-}
426{-# INLINE zipAsyncM #-}
427zipAsync, zipAsyncM :: S.MonadAsync m => Stream m Int -> m ()
431 let src1 = fromJust r
432 transform (S.zipAsyncWith (,) src src1)
435 let src1 = fromJust r
436 transform (S.zipAsyncWithM (curry return) src src1)
441eqBy :: (Monad m, P.Eq a) => Stream m a -> m P.Bool
442eqBy src = S.eqBy (==) src src
445cmpBy :: (Monad m, P.Ord a) => Stream m a -> m P.Ordering
446cmpBy src = S.cmpBy P.compare src src
448-------------------------------------------------------------------------------
450-------------------------------------------------------------------------------
452{-# INLINE scanMap #-}
453{-# INLINE dropMap #-}
454{-# INLINE dropScan #-}
455{-# INLINE takeDrop #-}
456{-# INLINE takeScan #-}
457{-# INLINE takeMap #-}
458{-# INLINE filterDrop #-}
459{-# INLINE filterTake #-}
460{-# INLINE filterScan #-}
461{-# INLINE filterScanl1 #-}
462{-# INLINE filterMap #-}
463scanMap, dropMap, dropScan, takeDrop, takeScan, takeMap, filterDrop,
464 filterTake, filterScan, filterScanl1, filterMap
465 :: Monad m => Int -> Stream m Int -> m ()
467scanMap n = composeN n $ S.map (subtract 1) . S.scanl' (+) 0
468dropMap n = composeN n $ S.map (subtract 1) . S.drop 1
469dropScan n = composeN n $ S.scanl' (+) 0 . S.drop 1
470takeDrop n = composeN n $ S.drop 1 . S.take maxValue
471takeScan n = composeN n $ S.scanl' (+) 0 . S.take maxValue
472takeMap n = composeN n $ S.map (subtract 1) . S.take maxValue
473filterDrop n = composeN n $ S.drop 1 . S.filter (<= maxValue)
474filterTake n = composeN n $ S.take maxValue . S.filter (<= maxValue)
475filterScan n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxBound)
476filterScanl1 n = composeN n $ S.scanl1' (+) . S.filter (<= maxBound)
477filterMap n = composeN n $ S.map (subtract 1) . S.filter (<= maxValue)
479-------------------------------------------------------------------------------
480-- Pure stream operations
481-------------------------------------------------------------------------------
483{-# INLINE eqInstance #-}
484eqInstance :: Stream Identity Int -> Bool
485eqInstance src = src == src
487{-# INLINE eqInstanceNotEq #-}
488eqInstanceNotEq :: Stream Identity Int -> Bool
489eqInstanceNotEq src = src P./= src
491{-# INLINE ordInstance #-}
492ordInstance :: Stream Identity Int -> Bool
493ordInstance src = src P.< src
495{-# INLINE ordInstanceMin #-}
496ordInstanceMin :: Stream Identity Int -> Stream Identity Int
497ordInstanceMin src = P.min src src
499{-# INLINE showInstance #-}
500showInstance :: Stream Identity Int -> P.String
501showInstance src = P.showsPrec 0 src ""
503{-# INLINE showInstanceList #-}
504showInstanceList :: Stream Identity Int -> P.String
505showInstanceList src = P.showsPrec 0 (GHC.toList src P.++ [2..value]) ""
507{-# INLINE readInstance #-}
508readInstance :: Stream Identity Int -> Stream Identity Int
510 let r = P.readsPrec 0 ("fromList [1"
511 P.++ P.concat (P.replicate value ",1") P.++ "]")
513 [(x,"")] -> src S.<> x
514 _ -> P.error "readInstance: no parse"
516{-# INLINE pureFoldl' #-}
517pureFoldl' :: Stream Identity Int -> Int
518pureFoldl' = runIdentity . S.foldl' (+) 0
520{-# INLINE foldableFoldl' #-}
521foldableFoldl' :: Stream Identity Int -> Int
522foldableFoldl' = F.foldl' (+) 0
524{-# INLINE foldableSum #-}
525foldableSum :: Stream Identity Int -> Int
528{-# INLINE traversableMapM #-}
529traversableMapM :: Stream Identity Int -> IO (Stream Identity Int)
530traversableMapM = P.mapM return
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list