2-- Module : BenchmarkOps
3-- Copyright : (c) 2018 Harendra Kumar
6-- Maintainer : harendra.kumar@gmail.com
9{-# LANGUAGE FlexibleContexts #-}
10{-# LANGUAGE ScopedTypeVariables #-}
11{-# LANGUAGE DeriveAnyClass #-}
12{-# LANGUAGE DeriveGeneric #-}
16import Control.Monad (when)
17import Data.Functor.Identity (Identity, runIdentity)
18import Data.Maybe (fromJust)
20 (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=), (==), (>=),
21 subtract, undefined, Maybe(..), odd, Bool, not, (>>=), mapM_, curry,
22 maxBound, div, IO, compare, Double, fromIntegral, Integer, (<$>),
24import qualified Prelude as P
25import qualified Data.Foldable as F
26import qualified GHC.Exts as GHC
27import Control.DeepSeq (NFData)
28import GHC.Generics (Generic)
30import qualified Streamly as S hiding (foldMapWith, runStream)
31import qualified Streamly.Prelude as S
41-------------------------------------------------------------------------------
43-------------------------------------------------------------------------------
45-------------------------------------------------------------------------------
46-- Stream generation and elimination
47-------------------------------------------------------------------------------
49type Stream m a = S.SerialT m a
52source :: (S.MonadAsync m, S.IsStream t) => Int -> t m Int
53source n = sourceUnfoldrM n
55{-# INLINE sourceIntFromTo #-}
56sourceIntFromTo :: (Monad m, S.IsStream t) => Int -> t m Int
57sourceIntFromTo n = S.enumerateFromTo n (n + value)
59{-# INLINE sourceIntFromThenTo #-}
60sourceIntFromThenTo :: (Monad m, S.IsStream t) => Int -> t m Int
61sourceIntFromThenTo n = S.enumerateFromThenTo n (n + 1) (n + value)
63{-# INLINE sourceFracFromTo #-}
64sourceFracFromTo :: (Monad m, S.IsStream t) => Int -> t m Double
66 S.enumerateFromTo (fromIntegral n) (fromIntegral (n + value))
68{-# INLINE sourceFracFromThenTo #-}
69sourceFracFromThenTo :: (Monad m, S.IsStream t) => Int -> t m Double
70sourceFracFromThenTo n = S.enumerateFromThenTo (fromIntegral n)
71 (fromIntegral n + 1.0001) (fromIntegral (n + value))
73{-# INLINE sourceIntegerFromStep #-}
74sourceIntegerFromStep :: (Monad m, S.IsStream t) => Int -> t m Integer
75sourceIntegerFromStep n =
76 S.take value $ S.enumerateFromThen (fromIntegral n) (fromIntegral n + 1)
78{-# INLINE sourceFromList #-}
79sourceFromList :: (Monad m, S.IsStream t) => Int -> t m Int
80sourceFromList n = S.fromList [n..n+value]
82{-# INLINE sourceFromListM #-}
83sourceFromListM :: (S.MonadAsync m, S.IsStream t) => Int -> t m Int
84sourceFromListM n = S.fromListM (Prelude.fmap return [n..n+value])
86{-# INLINE sourceFromFoldable #-}
87sourceFromFoldable :: S.IsStream t => Int -> t m Int
88sourceFromFoldable n = S.fromFoldable [n..n+value]
90{-# INLINE sourceFromFoldableM #-}
91sourceFromFoldableM :: (S.IsStream t, S.MonadAsync m) => Int -> t m Int
92sourceFromFoldableM n = S.fromFoldableM (Prelude.fmap return [n..n+value])
94{-# INLINE sourceFoldMapWith #-}
95sourceFoldMapWith :: (S.IsStream t, S.Semigroup (t m Int))
97sourceFoldMapWith n = S.foldMapWith (S.<>) S.yield [n..n+value]
99{-# INLINE sourceFoldMapWithM #-}
100sourceFoldMapWithM :: (S.IsStream t, Monad m, S.Semigroup (t m Int))
102sourceFoldMapWithM n = S.foldMapWith (S.<>) (S.yieldM . return) [n..n+value]
104{-# INLINE sourceFoldMapM #-}
105sourceFoldMapM :: (S.IsStream t, Monad m, P.Monoid (t m Int))
107sourceFoldMapM n = F.foldMap (S.yieldM . return) [n..n+value]
109{-# INLINE sourceUnfoldr #-}
110sourceUnfoldr :: (Monad m, S.IsStream t) => Int -> t m Int
111sourceUnfoldr n = S.unfoldr step n
116 else Just (cnt, cnt + 1)
118{-# INLINE sourceUnfoldrM #-}
119sourceUnfoldrM :: (S.IsStream t, S.MonadAsync m) => Int -> t m Int
120sourceUnfoldrM n = S.unfoldrM step n
125 else return (Just (cnt, cnt + 1))
127{-# INLINE sourceUnfoldrMN #-}
128sourceUnfoldrMN :: (S.IsStream t, S.MonadAsync m) => Int -> Int -> t m Int
129sourceUnfoldrMN m n = S.unfoldrM step n
134 else return (Just (cnt, cnt + 1))
136{-# INLINE sourceUnfoldrMAction #-}
137sourceUnfoldrMAction :: (S.IsStream t, S.MonadAsync m) => Int -> t m (m Int)
138sourceUnfoldrMAction n = S.serially $ S.unfoldrM step n
143 else return (Just (return cnt, cnt + 1))
145-------------------------------------------------------------------------------
146-- Pure stream generation
147-------------------------------------------------------------------------------
149{-# INLINE sourceIsList #-}
150sourceIsList :: Int -> S.SerialT Identity Int
151sourceIsList n = GHC.fromList [n..n+value]
153{-# INLINE sourceIsString #-}
154sourceIsString :: Int -> S.SerialT Identity P.Char
155sourceIsString n = GHC.fromString (P.replicate (n + value) 'a')
157-------------------------------------------------------------------------------
159-------------------------------------------------------------------------------
161{-# INLINE runStream #-}
162runStream :: Monad m => Stream m a -> m ()
166toList :: Monad m => Stream m Int -> m [Int]
170{-# INLINE maximum #-}
171{-# INLINE minimum #-}
173{-# INLINE findIndex #-}
174{-# INLINE elemIndex #-}
175{-# INLINE foldl1'Reduce #-}
176head, last, minimum, maximum, find, findIndex, elemIndex, foldl1'Reduce
177 :: Monad m => Stream m Int -> m (Maybe Int)
179{-# INLINE minimumBy #-}
180{-# INLINE maximumBy #-}
181minimumBy, maximumBy :: Monad m => Stream m Int -> m (Maybe Int)
183{-# INLINE foldl'Reduce #-}
184{-# INLINE foldl'ReduceMap #-}
185{-# INLINE foldlM'Reduce #-}
186{-# INLINE foldrMReduce #-}
189{-# INLINE product #-}
190foldl'Reduce, foldl'ReduceMap, foldlM'Reduce, foldrMReduce, length, sum, product
192 => Stream m Int -> m Int
194{-# INLINE foldl'Build #-}
195{-# INLINE foldlM'Build #-}
196{-# INLINE foldrMBuild #-}
197foldrMBuild, foldl'Build, foldlM'Build
199 => Stream m Int -> m [Int]
207{-# INLINE notElem #-}
208null, elem, notElem, all, any, and, or :: Monad m => Stream m Int -> m Bool
211toNull :: Monad m => (t m a -> S.SerialT m a) -> t m a -> m ()
212toNull t = runStream . t
215uncons :: Monad m => Stream m Int -> m ()
220 Just (_, t) -> uncons t
223init :: Monad m => Stream m a -> m ()
224init s = S.init s >>= Prelude.mapM_ S.drain
227tail :: Monad m => Stream m a -> m ()
228tail s = S.tail s >>= Prelude.mapM_ tail
230{-# INLINE nullHeadTail #-}
231nullHeadTail :: Monad m => Stream m Int -> m ()
236 S.tail s >>= Prelude.mapM_ nullHeadTail
239mapM_ :: Monad m => Stream m Int -> m ()
240mapM_ = S.mapM_ (\_ -> return ())
245{-# INLINE toRevList #-}
246toRevList :: Monad m => Stream m Int -> m [Int]
247toRevList = S.toRevList
250foldrMBuild = S.foldrM (\x xs -> xs >>= return . (x :)) (return [])
251foldl'Build = S.foldl' (flip (:)) []
252foldlM'Build = S.foldlM' (\xs x -> return $ x : xs) []
254foldrMReduce = S.foldrM (\x xs -> xs >>= return . (x +)) (return 0)
255foldl'Reduce = S.foldl' (+) 0
256foldl'ReduceMap = P.fmap (+1) . S.foldl' (+) 0
257foldl1'Reduce = S.foldl1' (+)
258foldlM'Reduce = S.foldlM' (\xs a -> return $ a + xs) 0
263elem = S.elem maxValue
264notElem = S.notElem maxValue
266all = S.all (<= maxValue)
267any = S.any (> maxValue)
268and = S.and . S.map (<= maxValue)
269or = S.or . S.map (> maxValue)
270find = S.find (== maxValue)
271findIndex = S.findIndex (== maxValue)
272elemIndex = S.elemIndex maxValue
277minimumBy = S.minimumBy compare
278maximumBy = S.maximumBy compare
280-------------------------------------------------------------------------------
282-------------------------------------------------------------------------------
284{-# INLINE transform #-}
285transform :: Monad m => Stream m a -> m ()
288{-# INLINE composeN #-}
291 => Int -> (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
295 2 -> transform . f . f
296 3 -> transform . f . f . f
297 4 -> transform . f . f . f . f
300-- polymorphic stream version of composeN
301{-# INLINE composeN' #-}
303 :: (S.IsStream t, Monad m)
304 => Int -> (t m Int -> Stream m Int) -> t m Int -> m ()
308 2 -> transform . f . S.adapt . f
309 3 -> transform . f . S.adapt . f . S.adapt . f
310 4 -> transform . f . S.adapt . f . S.adapt . f . S.adapt . f
314{-# INLINE scanl1' #-}
317{-# INLINE mapMaybe #-}
318{-# INLINE filterEven #-}
319{-# INLINE filterAllOut #-}
320{-# INLINE filterAllIn #-}
321{-# INLINE takeOne #-}
322{-# INLINE takeAll #-}
323{-# INLINE takeWhileTrue #-}
324{-# INLINE takeWhileMTrue #-}
325{-# INLINE dropOne #-}
326{-# INLINE dropAll #-}
327{-# INLINE dropWhileTrue #-}
328{-# INLINE dropWhileMTrue #-}
329{-# INLINE dropWhileFalse #-}
330{-# INLINE findIndices #-}
331{-# INLINE elemIndices #-}
332{-# INLINE insertBy #-}
333{-# INLINE deleteBy #-}
334{-# INLINE reverse #-}
335{-# INLINE reverse' #-}
337{-# INLINE foldrSMap #-}
339{-# INLINE foldrTMap #-}
340scan, scanl1', map, fmap, mapMaybe, filterEven, filterAllOut,
341 filterAllIn, takeOne, takeAll, takeWhileTrue, takeWhileMTrue, dropOne,
342 dropAll, dropWhileTrue, dropWhileMTrue, dropWhileFalse,
343 findIndices, elemIndices, insertBy, deleteBy, reverse, reverse',
344 foldrS, foldrSMap, foldrT, foldrTMap
346 => Int -> Stream m Int -> m ()
348{-# INLINE mapMaybeM #-}
349mapMaybeM :: S.MonadAsync m => Int -> Stream m Int -> m ()
354mapM, map' :: (S.IsStream t, S.MonadAsync m)
355 => (t m Int -> S.SerialT m Int) -> Int -> t m Int -> m ()
357fmap' :: (S.IsStream t, S.MonadAsync m, P.Functor (t m))
358 => (t m Int -> S.SerialT m Int) -> Int -> t m Int -> m ()
360{-# INLINE sequence #-}
361sequence :: (S.IsStream t, S.MonadAsync m)
362 => (t m Int -> S.SerialT m Int) -> t m (m Int) -> m ()
364scan n = composeN n $ S.scanl' (+) 0
365scanl1' n = composeN n $ S.scanl1' (+)
366fmap n = composeN n $ Prelude.fmap (+1)
367fmap' t n = composeN' n $ t . Prelude.fmap (+1)
368map n = composeN n $ S.map (+1)
369map' t n = composeN' n $ t . S.map (+1)
370mapM t n = composeN' n $ t . S.mapM return
371mapMaybe n = composeN n $ S.mapMaybe
372 (\x -> if Prelude.odd x then Nothing else Just x)
373mapMaybeM n = composeN n $ S.mapMaybeM
374 (\x -> if Prelude.odd x then return Nothing else return $ Just x)
375sequence t = transform . t . S.sequence
376filterEven n = composeN n $ S.filter even
377filterAllOut n = composeN n $ S.filter (> maxValue)
378filterAllIn n = composeN n $ S.filter (<= maxValue)
379takeOne n = composeN n $ S.take 1
380takeAll n = composeN n $ S.take maxValue
381takeWhileTrue n = composeN n $ S.takeWhile (<= maxValue)
382takeWhileMTrue n = composeN n $ S.takeWhileM (return . (<= maxValue))
383dropOne n = composeN n $ S.drop 1
384dropAll n = composeN n $ S.drop maxValue
385dropWhileTrue n = composeN n $ S.dropWhile (<= maxValue)
386dropWhileMTrue n = composeN n $ S.dropWhileM (return . (<= maxValue))
387dropWhileFalse n = composeN n $ S.dropWhile (> maxValue)
388findIndices n = composeN n $ S.findIndices (== maxValue)
389elemIndices n = composeN n $ S.elemIndices maxValue
390insertBy n = composeN n $ S.insertBy compare maxValue
391deleteBy n = composeN n $ S.deleteBy (>=) maxValue
392reverse n = composeN n $ S.reverse
393reverse' n = composeN n $ S.reverse'
394foldrS n = composeN n $ S.foldrS S.cons S.nil
395foldrSMap n = composeN n $ S.foldrS (\x xs -> x + 1 `S.cons` xs) S.nil
396foldrT n = composeN n $ S.foldrT S.cons S.nil
397foldrTMap n = composeN n $ S.foldrT (\x xs -> x + 1 `S.cons` xs) S.nil
399-------------------------------------------------------------------------------
401-------------------------------------------------------------------------------
403iterStreamLen, maxIters :: Int
407{-# INLINE iterateSource #-}
410 => (Stream m Int -> Stream m Int) -> Int -> Int -> Stream m Int
411iterateSource g i n = f i (sourceUnfoldrMN iterStreamLen n)
414 f x m = g (f (x P.- 1) m)
416{-# INLINE iterateMapM #-}
417{-# INLINE iterateScan #-}
418{-# INLINE iterateScanl1 #-}
419{-# INLINE iterateFilterEven #-}
420{-# INLINE iterateTakeAll #-}
421{-# INLINE iterateDropOne #-}
422{-# INLINE iterateDropWhileFalse #-}
423{-# INLINE iterateDropWhileTrue #-}
424iterateMapM, iterateScan, iterateScanl1, iterateFilterEven, iterateTakeAll,
425 iterateDropOne, iterateDropWhileFalse, iterateDropWhileTrue
427 => Int -> Stream m Int
430iterateScan = iterateSource (S.scanl' (+) 0) (maxIters `div` 10)
432iterateScanl1 = iterateSource (S.scanl1' (+)) (maxIters `div` 10)
434iterateMapM = iterateSource (S.mapM return) maxIters
435iterateFilterEven = iterateSource (S.filter even) maxIters
436iterateTakeAll = iterateSource (S.take maxValue) maxIters
437iterateDropOne = iterateSource (S.drop 1) maxIters
438iterateDropWhileFalse = iterateSource (S.dropWhile (> maxValue)) maxIters
439iterateDropWhileTrue = iterateSource (S.dropWhile (<= maxValue)) maxIters
441-------------------------------------------------------------------------------
443-------------------------------------------------------------------------------
447{-# INLINE mergeBy #-}
448zip, zipM, mergeBy :: Monad m => Stream m Int -> m ()
452 let src1 = fromJust r
453 transform (S.zipWith (,) src src1)
456 let src1 = fromJust r
457 transform (S.zipWithM (curry return) src src1)
461 let src1 = fromJust r
462 transform (S.mergeBy P.compare src src1)
464{-# INLINE isPrefixOf #-}
465{-# INLINE isSubsequenceOf #-}
466isPrefixOf, isSubsequenceOf :: Monad m => Stream m Int -> m Bool
468isPrefixOf src = S.isPrefixOf src src
469isSubsequenceOf src = S.isSubsequenceOf src src
471{-# INLINE stripPrefix #-}
472stripPrefix :: Monad m => Stream m Int -> m ()
474 _ <- S.stripPrefix src src
477{-# INLINE zipAsync #-}
478{-# INLINE zipAsyncM #-}
479{-# INLINE zipAsyncAp #-}
480zipAsync, zipAsyncAp, zipAsyncM :: S.MonadAsync m => Stream m Int -> m ()
484 let src1 = fromJust r
485 transform (S.zipAsyncWith (,) src src1)
489 let src1 = fromJust r
490 transform (S.zipAsyncWithM (curry return) src src1)
494 let src1 = fromJust r
495 transform (S.zipAsyncly $ (,) <$> S.serially src
499eqBy :: (Monad m, P.Eq a) => Stream m a -> m P.Bool
500eqBy src = S.eqBy (==) src src
503cmpBy :: (Monad m, P.Ord a) => Stream m a -> m P.Ordering
504cmpBy src = S.cmpBy P.compare src src
506concatStreamLen, maxNested :: Int
510{-# INLINE concatMap #-}
511concatMap :: S.MonadAsync m => Int -> Stream m Int
512concatMap n = S.concatMap (\_ -> sourceUnfoldrMN maxNested n)
513 (sourceUnfoldrMN concatStreamLen n)
515-------------------------------------------------------------------------------
517-------------------------------------------------------------------------------
519{-# INLINE scanMap #-}
520{-# INLINE dropMap #-}
521{-# INLINE dropScan #-}
522{-# INLINE takeDrop #-}
523{-# INLINE takeScan #-}
524{-# INLINE takeMap #-}
525{-# INLINE filterDrop #-}
526{-# INLINE filterTake #-}
527{-# INLINE filterScan #-}
528{-# INLINE filterScanl1 #-}
529{-# INLINE filterMap #-}
530scanMap, dropMap, dropScan, takeDrop, takeScan, takeMap, filterDrop,
531 filterTake, filterScan, filterScanl1, filterMap
532 :: Monad m => Int -> Stream m Int -> m ()
534scanMap n = composeN n $ S.map (subtract 1) . S.scanl' (+) 0
535dropMap n = composeN n $ S.map (subtract 1) . S.drop 1
536dropScan n = composeN n $ S.scanl' (+) 0 . S.drop 1
537takeDrop n = composeN n $ S.drop 1 . S.take maxValue
538takeScan n = composeN n $ S.scanl' (+) 0 . S.take maxValue
539takeMap n = composeN n $ S.map (subtract 1) . S.take maxValue
540filterDrop n = composeN n $ S.drop 1 . S.filter (<= maxValue)
541filterTake n = composeN n $ S.take maxValue . S.filter (<= maxValue)
542filterScan n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxBound)
543filterScanl1 n = composeN n $ S.scanl1' (+) . S.filter (<= maxBound)
544filterMap n = composeN n $ S.map (subtract 1) . S.filter (<= maxValue)
546data Pair a b = Pair !a !b deriving (Generic, NFData)
548{-# INLINE sumProductFold #-}
549sumProductFold :: Monad m => Stream m Int -> m (Int, Int)
550sumProductFold = S.foldl' (\(s,p) x -> (s + x, p P.* x)) (0,1)
552{-# INLINE sumProductScan #-}
553sumProductScan :: Monad m => Stream m Int -> m (Pair Int Int)
554sumProductScan = S.foldl' (\(Pair _ p) (s0,x) -> Pair s0 (p P.* x)) (Pair 0 1)
555 . S.scanl' (\(s,_) x -> (s + x,x)) (0,0)
557-------------------------------------------------------------------------------
558-- Pure stream operations
559-------------------------------------------------------------------------------
561{-# INLINE eqInstance #-}
562eqInstance :: Stream Identity Int -> Bool
563eqInstance src = src == src
565{-# INLINE eqInstanceNotEq #-}
566eqInstanceNotEq :: Stream Identity Int -> Bool
567eqInstanceNotEq src = src P./= src
569{-# INLINE ordInstance #-}
570ordInstance :: Stream Identity Int -> Bool
571ordInstance src = src P.< src
573{-# INLINE ordInstanceMin #-}
574ordInstanceMin :: Stream Identity Int -> Stream Identity Int
575ordInstanceMin src = P.min src src
577{-# INLINE showInstance #-}
578showInstance :: Stream Identity Int -> P.String
579showInstance src = P.show src
581{-# INLINE showInstanceList #-}
582showInstanceList :: [Int] -> P.String
583showInstanceList src = P.show src
585{-# INLINE readInstance #-}
586readInstance :: P.String -> Stream Identity Int
591 _ -> P.error "readInstance: no parse"
593{-# INLINE readInstanceList #-}
594readInstanceList :: P.String -> [Int]
595readInstanceList str =
599 _ -> P.error "readInstance: no parse"
601{-# INLINE pureFoldl' #-}
602pureFoldl' :: Stream Identity Int -> Int
603pureFoldl' = runIdentity . S.foldl' (+) 0
605{-# INLINE foldableFoldl' #-}
606foldableFoldl' :: Stream Identity Int -> Int
607foldableFoldl' = F.foldl' (+) 0
609{-# INLINE foldableSum #-}
610foldableSum :: Stream Identity Int -> Int
613{-# INLINE traversableMapM #-}
614traversableMapM :: Stream Identity Int -> IO (Stream Identity Int)
615traversableMapM = 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