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 Control.Monad.IO.Class (MonadIO)
18import Data.Functor.Identity (Identity, runIdentity)
19import Data.Maybe (fromJust)
21 (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=), (==), (>=),
22 subtract, undefined, Maybe(..), odd, Bool, not, (>>=), mapM_, curry,
23 maxBound, div, IO, compare, Double, fromIntegral, Integer, (<$>),
24 (<*>), flip, (**), (/))
25import qualified Prelude as P
26import qualified Data.Foldable as F
27import qualified GHC.Exts as GHC
28import Control.DeepSeq (NFData)
29import GHC.Generics (Generic)
31import qualified Streamly as S hiding (foldMapWith, runStream)
32import qualified Streamly.Prelude as S
34value, maxValue, value2 :: Int
41value2 = P.round (P.fromIntegral value**(1/2::P.Double)) -- double nested loop
43-------------------------------------------------------------------------------
45-------------------------------------------------------------------------------
47-------------------------------------------------------------------------------
48-- Stream generation and elimination
49-------------------------------------------------------------------------------
51type Stream m a = S.SerialT m a
54source :: (S.MonadAsync m, S.IsStream t) => Int -> t m Int
55source n = sourceUnfoldrM n
57{-# INLINE sourceIntFromTo #-}
58sourceIntFromTo :: (Monad m, S.IsStream t) => Int -> t m Int
59sourceIntFromTo n = S.enumerateFromTo n (n + value)
61{-# INLINE sourceIntFromThenTo #-}
62sourceIntFromThenTo :: (Monad m, S.IsStream t) => Int -> t m Int
63sourceIntFromThenTo n = S.enumerateFromThenTo n (n + 1) (n + value)
65{-# INLINE sourceFracFromTo #-}
66sourceFracFromTo :: (Monad m, S.IsStream t) => Int -> t m Double
68 S.enumerateFromTo (fromIntegral n) (fromIntegral (n + value))
70{-# INLINE sourceFracFromThenTo #-}
71sourceFracFromThenTo :: (Monad m, S.IsStream t) => Int -> t m Double
72sourceFracFromThenTo n = S.enumerateFromThenTo (fromIntegral n)
73 (fromIntegral n + 1.0001) (fromIntegral (n + value))
75{-# INLINE sourceIntegerFromStep #-}
76sourceIntegerFromStep :: (Monad m, S.IsStream t) => Int -> t m Integer
77sourceIntegerFromStep n =
78 S.take value $ S.enumerateFromThen (fromIntegral n) (fromIntegral n + 1)
80{-# INLINE sourceFromList #-}
81sourceFromList :: (Monad m, S.IsStream t) => Int -> t m Int
82sourceFromList n = S.fromList [n..n+value]
84{-# INLINE sourceFromListM #-}
85sourceFromListM :: (S.MonadAsync m, S.IsStream t) => Int -> t m Int
86sourceFromListM n = S.fromListM (Prelude.fmap return [n..n+value])
88{-# INLINE sourceFromFoldable #-}
89sourceFromFoldable :: S.IsStream t => Int -> t m Int
90sourceFromFoldable n = S.fromFoldable [n..n+value]
92{-# INLINE sourceFromFoldableM #-}
93sourceFromFoldableM :: (S.IsStream t, S.MonadAsync m) => Int -> t m Int
94sourceFromFoldableM n = S.fromFoldableM (Prelude.fmap return [n..n+value])
96{-# INLINE sourceFoldMapWith #-}
97sourceFoldMapWith :: (S.IsStream t, S.Semigroup (t m Int))
99sourceFoldMapWith n = S.foldMapWith (S.<>) S.yield [n..n+value]
101{-# INLINE sourceFoldMapWithM #-}
102sourceFoldMapWithM :: (S.IsStream t, Monad m, S.Semigroup (t m Int))
104sourceFoldMapWithM n = S.foldMapWith (S.<>) (S.yieldM . return) [n..n+value]
106{-# INLINE sourceFoldMapM #-}
107sourceFoldMapM :: (S.IsStream t, Monad m, P.Monoid (t m Int))
109sourceFoldMapM n = F.foldMap (S.yieldM . return) [n..n+value]
111{-# INLINE sourceUnfoldr #-}
112sourceUnfoldr :: (Monad m, S.IsStream t) => Int -> t m Int
113sourceUnfoldr n = S.unfoldr step n
118 else Just (cnt, cnt + 1)
120{-# INLINE sourceUnfoldrM #-}
121sourceUnfoldrM :: (S.IsStream t, S.MonadAsync m) => Int -> t m Int
122sourceUnfoldrM n = S.unfoldrM step n
127 else return (Just (cnt, cnt + 1))
129{-# INLINE sourceUnfoldrMN #-}
130sourceUnfoldrMN :: (S.IsStream t, S.MonadAsync m) => Int -> Int -> t m Int
131sourceUnfoldrMN m n = S.unfoldrM step n
136 else return (Just (cnt, cnt + 1))
138{-# INLINE sourceUnfoldrMAction #-}
139sourceUnfoldrMAction :: (S.IsStream t, S.MonadAsync m) => Int -> t m (m Int)
140sourceUnfoldrMAction n = S.serially $ S.unfoldrM step n
145 else return (Just (return cnt, cnt + 1))
147-------------------------------------------------------------------------------
148-- Pure stream generation
149-------------------------------------------------------------------------------
151{-# INLINE sourceIsList #-}
152sourceIsList :: Int -> S.SerialT Identity Int
153sourceIsList n = GHC.fromList [n..n+value]
155{-# INLINE sourceIsString #-}
156sourceIsString :: Int -> S.SerialT Identity P.Char
157sourceIsString n = GHC.fromString (P.replicate (n + value) 'a')
159-------------------------------------------------------------------------------
161-------------------------------------------------------------------------------
163{-# INLINE runStream #-}
164runStream :: Monad m => Stream m a -> m ()
168toList :: Monad m => Stream m Int -> m [Int]
172{-# INLINE maximum #-}
173{-# INLINE minimum #-}
175{-# INLINE findIndex #-}
176{-# INLINE elemIndex #-}
177{-# INLINE foldl1'Reduce #-}
178head, last, minimum, maximum, find, findIndex, elemIndex, foldl1'Reduce
179 :: Monad m => Stream m Int -> m (Maybe Int)
181{-# INLINE minimumBy #-}
182{-# INLINE maximumBy #-}
183minimumBy, maximumBy :: Monad m => Stream m Int -> m (Maybe Int)
185{-# INLINE foldl'Reduce #-}
186{-# INLINE foldl'ReduceMap #-}
187{-# INLINE foldlM'Reduce #-}
188{-# INLINE foldrMReduce #-}
191{-# INLINE product #-}
192foldl'Reduce, foldl'ReduceMap, foldlM'Reduce, foldrMReduce, length, sum, product
194 => Stream m Int -> m Int
196{-# INLINE foldl'Build #-}
197{-# INLINE foldlM'Build #-}
198{-# INLINE foldrMBuild #-}
199foldrMBuild, foldl'Build, foldlM'Build
201 => Stream m Int -> m [Int]
209{-# INLINE notElem #-}
210null, elem, notElem, all, any, and, or :: Monad m => Stream m Int -> m Bool
213toNull :: Monad m => (t m a -> S.SerialT m a) -> t m a -> m ()
214toNull t = runStream . t
217uncons :: Monad m => Stream m Int -> m ()
222 Just (_, t) -> uncons t
225init :: Monad m => Stream m a -> m ()
226init s = S.init s >>= Prelude.mapM_ S.drain
229tail :: Monad m => Stream m a -> m ()
230tail s = S.tail s >>= Prelude.mapM_ tail
232{-# INLINE nullHeadTail #-}
233nullHeadTail :: Monad m => Stream m Int -> m ()
238 S.tail s >>= Prelude.mapM_ nullHeadTail
241mapM_ :: Monad m => Stream m Int -> m ()
242mapM_ = S.mapM_ (\_ -> return ())
247{-# INLINE toRevList #-}
248toRevList :: Monad m => Stream m Int -> m [Int]
249toRevList = S.toRevList
252foldrMBuild = S.foldrM (\x xs -> xs >>= return . (x :)) (return [])
253foldl'Build = S.foldl' (flip (:)) []
254foldlM'Build = S.foldlM' (\xs x -> return $ x : xs) []
256foldrMReduce = S.foldrM (\x xs -> xs >>= return . (x +)) (return 0)
257foldl'Reduce = S.foldl' (+) 0
258foldl'ReduceMap = P.fmap (+1) . S.foldl' (+) 0
259foldl1'Reduce = S.foldl1' (+)
260foldlM'Reduce = S.foldlM' (\xs a -> return $ a + xs) 0
265elem = S.elem maxValue
266notElem = S.notElem maxValue
268all = S.all (<= maxValue)
269any = S.any (> maxValue)
270and = S.and . S.map (<= maxValue)
271or = S.or . S.map (> maxValue)
272find = S.find (== maxValue)
273findIndex = S.findIndex (== maxValue)
274elemIndex = S.elemIndex maxValue
279minimumBy = S.minimumBy compare
280maximumBy = S.maximumBy compare
282-------------------------------------------------------------------------------
284-------------------------------------------------------------------------------
286{-# INLINE transform #-}
287transform :: Monad m => Stream m a -> m ()
290{-# INLINE composeN #-}
293 => Int -> (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
297 2 -> transform . f . f
298 3 -> transform . f . f . f
299 4 -> transform . f . f . f . f
302-- polymorphic stream version of composeN
303{-# INLINE composeN' #-}
305 :: (S.IsStream t, Monad m)
306 => Int -> (t m Int -> Stream m Int) -> t m Int -> m ()
310 2 -> transform . f . S.adapt . f
311 3 -> transform . f . S.adapt . f . S.adapt . f
312 4 -> transform . f . S.adapt . f . S.adapt . f . S.adapt . f
316{-# INLINE scanl1' #-}
319{-# INLINE mapMaybe #-}
320{-# INLINE filterEven #-}
321{-# INLINE filterAllOut #-}
322{-# INLINE filterAllIn #-}
323{-# INLINE takeOne #-}
324{-# INLINE takeAll #-}
325{-# INLINE takeWhileTrue #-}
326{-# INLINE takeWhileMTrue #-}
327{-# INLINE dropOne #-}
328{-# INLINE dropAll #-}
329{-# INLINE dropWhileTrue #-}
330{-# INLINE dropWhileMTrue #-}
331{-# INLINE dropWhileFalse #-}
332{-# INLINE findIndices #-}
333{-# INLINE elemIndices #-}
334{-# INLINE insertBy #-}
335{-# INLINE deleteBy #-}
336{-# INLINE reverse #-}
337{-# INLINE reverse' #-}
339{-# INLINE foldrSMap #-}
341{-# INLINE foldrTMap #-}
342scan, scanl1', map, fmap, mapMaybe, filterEven, filterAllOut,
343 filterAllIn, takeOne, takeAll, takeWhileTrue, takeWhileMTrue, dropOne,
344 dropAll, dropWhileTrue, dropWhileMTrue, dropWhileFalse,
345 findIndices, elemIndices, insertBy, deleteBy, reverse, reverse',
346 foldrS, foldrSMap, foldrT, foldrTMap
348 => Int -> Stream m Int -> m ()
350{-# INLINE mapMaybeM #-}
351mapMaybeM :: S.MonadAsync m => Int -> Stream m Int -> m ()
356mapM, map' :: (S.IsStream t, S.MonadAsync m)
357 => (t m Int -> S.SerialT m Int) -> Int -> t m Int -> m ()
359fmap' :: (S.IsStream t, S.MonadAsync m, P.Functor (t m))
360 => (t m Int -> S.SerialT m Int) -> Int -> t m Int -> m ()
362{-# INLINE sequence #-}
363sequence :: (S.IsStream t, S.MonadAsync m)
364 => (t m Int -> S.SerialT m Int) -> t m (m Int) -> m ()
366scan n = composeN n $ S.scanl' (+) 0
367scanl1' n = composeN n $ S.scanl1' (+)
368fmap n = composeN n $ Prelude.fmap (+1)
369fmap' t n = composeN' n $ t . Prelude.fmap (+1)
370map n = composeN n $ S.map (+1)
371map' t n = composeN' n $ t . S.map (+1)
372mapM t n = composeN' n $ t . S.mapM return
373mapMaybe n = composeN n $ S.mapMaybe
374 (\x -> if Prelude.odd x then Nothing else Just x)
375mapMaybeM n = composeN n $ S.mapMaybeM
376 (\x -> if Prelude.odd x then return Nothing else return $ Just x)
377sequence t = transform . t . S.sequence
378filterEven n = composeN n $ S.filter even
379filterAllOut n = composeN n $ S.filter (> maxValue)
380filterAllIn n = composeN n $ S.filter (<= maxValue)
381takeOne n = composeN n $ S.take 1
382takeAll n = composeN n $ S.take maxValue
383takeWhileTrue n = composeN n $ S.takeWhile (<= maxValue)
384takeWhileMTrue n = composeN n $ S.takeWhileM (return . (<= maxValue))
385dropOne n = composeN n $ S.drop 1
386dropAll n = composeN n $ S.drop maxValue
387dropWhileTrue n = composeN n $ S.dropWhile (<= maxValue)
388dropWhileMTrue n = composeN n $ S.dropWhileM (return . (<= maxValue))
389dropWhileFalse n = composeN n $ S.dropWhile (> maxValue)
390findIndices n = composeN n $ S.findIndices (== maxValue)
391elemIndices n = composeN n $ S.elemIndices maxValue
392insertBy n = composeN n $ S.insertBy compare maxValue
393deleteBy n = composeN n $ S.deleteBy (>=) maxValue
394reverse n = composeN n $ S.reverse
395reverse' n = composeN n $ S.reverse'
396foldrS n = composeN n $ S.foldrS S.cons S.nil
397foldrSMap n = composeN n $ S.foldrS (\x xs -> x + 1 `S.cons` xs) S.nil
398foldrT n = composeN n $ S.foldrT S.cons S.nil
399foldrTMap n = composeN n $ S.foldrT (\x xs -> x + 1 `S.cons` xs) S.nil
401-------------------------------------------------------------------------------
403-------------------------------------------------------------------------------
405iterStreamLen, maxIters :: Int
409{-# INLINE iterateSource #-}
412 => (Stream m Int -> Stream m Int) -> Int -> Int -> Stream m Int
413iterateSource g i n = f i (sourceUnfoldrMN iterStreamLen n)
416 f x m = g (f (x P.- 1) m)
418{-# INLINE iterateMapM #-}
419{-# INLINE iterateScan #-}
420{-# INLINE iterateScanl1 #-}
421{-# INLINE iterateFilterEven #-}
422{-# INLINE iterateTakeAll #-}
423{-# INLINE iterateDropOne #-}
424{-# INLINE iterateDropWhileFalse #-}
425{-# INLINE iterateDropWhileTrue #-}
426iterateMapM, iterateScan, iterateScanl1, iterateFilterEven, iterateTakeAll,
427 iterateDropOne, iterateDropWhileFalse, iterateDropWhileTrue
429 => Int -> Stream m Int
432iterateScan = iterateSource (S.scanl' (+) 0) (maxIters `div` 10)
434iterateScanl1 = iterateSource (S.scanl1' (+)) (maxIters `div` 10)
436iterateMapM = iterateSource (S.mapM return) maxIters
437iterateFilterEven = iterateSource (S.filter even) maxIters
438iterateTakeAll = iterateSource (S.take maxValue) maxIters
439iterateDropOne = iterateSource (S.drop 1) maxIters
440iterateDropWhileFalse = iterateSource (S.dropWhile (> maxValue)) maxIters
441iterateDropWhileTrue = iterateSource (S.dropWhile (<= maxValue)) maxIters
443-------------------------------------------------------------------------------
445-------------------------------------------------------------------------------
449{-# INLINE mergeBy #-}
450zip, zipM, mergeBy :: Monad m => Stream m Int -> m ()
454 let src1 = fromJust r
455 transform (S.zipWith (,) src src1)
458 let src1 = fromJust r
459 transform (S.zipWithM (curry return) src src1)
463 let src1 = fromJust r
464 transform (S.mergeBy P.compare src src1)
466{-# INLINE isPrefixOf #-}
467{-# INLINE isSubsequenceOf #-}
468isPrefixOf, isSubsequenceOf :: Monad m => Stream m Int -> m Bool
470isPrefixOf src = S.isPrefixOf src src
471isSubsequenceOf src = S.isSubsequenceOf src src
473{-# INLINE stripPrefix #-}
474stripPrefix :: Monad m => Stream m Int -> m ()
476 _ <- S.stripPrefix src src
479{-# INLINE zipAsync #-}
480{-# INLINE zipAsyncM #-}
481{-# INLINE zipAsyncAp #-}
482zipAsync, zipAsyncAp, zipAsyncM :: S.MonadAsync m => Stream m Int -> m ()
486 let src1 = fromJust r
487 transform (S.zipAsyncWith (,) src src1)
491 let src1 = fromJust r
492 transform (S.zipAsyncWithM (curry return) src src1)
496 let src1 = fromJust r
497 transform (S.zipAsyncly $ (,) <$> S.serially src
501eqBy :: (Monad m, P.Eq a) => Stream m a -> m P.Bool
502eqBy src = S.eqBy (==) src src
505cmpBy :: (Monad m, P.Ord a) => Stream m a -> m P.Ordering
506cmpBy src = S.cmpBy P.compare src src
508concatStreamLen, maxNested :: Int
509-- concatStreamLen = value2
514{-# INLINE concatMap #-}
515concatMap :: S.MonadAsync m => Int -> Stream m Int
516concatMap n = S.concatMap (\_ -> sourceUnfoldrMN maxNested n)
517 (sourceUnfoldrMN concatStreamLen n)
519-------------------------------------------------------------------------------
521-------------------------------------------------------------------------------
523{-# INLINE scanMap #-}
524{-# INLINE dropMap #-}
525{-# INLINE dropScan #-}
526{-# INLINE takeDrop #-}
527{-# INLINE takeScan #-}
528{-# INLINE takeMap #-}
529{-# INLINE filterDrop #-}
530{-# INLINE filterTake #-}
531{-# INLINE filterScan #-}
532{-# INLINE filterScanl1 #-}
533{-# INLINE filterMap #-}
534scanMap, dropMap, dropScan, takeDrop, takeScan, takeMap, filterDrop,
535 filterTake, filterScan, filterScanl1, filterMap
536 :: MonadIO m => Int -> Stream m Int -> m ()
538scanMap n = composeN n $ S.map (subtract 1) . S.scanl' (+) 0
539dropMap n = composeN n $ S.map (subtract 1) . S.drop 1
540dropScan n = composeN n $ S.scanl' (+) 0 . S.drop 1
541takeDrop n = composeN n $ S.drop 1 . S.take maxValue
542takeScan n = composeN n $ S.scanl' (+) 0 . S.take maxValue
543takeMap n = composeN n $ S.map (subtract 1) . S.take maxValue
544filterDrop n = composeN n $ S.drop 1 . S.filter (<= maxValue)
545filterTake n = composeN n $ S.take maxValue . S.filter (<= maxValue)
546filterScan n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxBound)
547filterScanl1 n = composeN n $ S.scanl1' (+) . S.filter (<= maxBound)
548filterMap n = composeN n $ S.map (subtract 1) . S.filter (<= maxValue)
550data Pair a b = Pair !a !b deriving (Generic, NFData)
552{-# INLINE sumProductFold #-}
553sumProductFold :: Monad m => Stream m Int -> m (Int, Int)
554sumProductFold = S.foldl' (\(s,p) x -> (s + x, p P.* x)) (0,1)
556{-# INLINE sumProductScan #-}
557sumProductScan :: Monad m => Stream m Int -> m (Pair Int Int)
558sumProductScan = S.foldl' (\(Pair _ p) (s0,x) -> Pair s0 (p P.* x)) (Pair 0 1)
559 . S.scanl' (\(s,_) x -> (s + x,x)) (0,0)
561-------------------------------------------------------------------------------
562-- Pure stream operations
563-------------------------------------------------------------------------------
565{-# INLINE eqInstance #-}
566eqInstance :: Stream Identity Int -> Bool
567eqInstance src = src == src
569{-# INLINE eqInstanceNotEq #-}
570eqInstanceNotEq :: Stream Identity Int -> Bool
571eqInstanceNotEq src = src P./= src
573{-# INLINE ordInstance #-}
574ordInstance :: Stream Identity Int -> Bool
575ordInstance src = src P.< src
577{-# INLINE ordInstanceMin #-}
578ordInstanceMin :: Stream Identity Int -> Stream Identity Int
579ordInstanceMin src = P.min src src
581{-# INLINE showInstance #-}
582showInstance :: Stream Identity Int -> P.String
583showInstance src = P.show src
585{-# INLINE showInstanceList #-}
586showInstanceList :: [Int] -> P.String
587showInstanceList src = P.show src
589{-# INLINE readInstance #-}
590readInstance :: P.String -> Stream Identity Int
595 _ -> P.error "readInstance: no parse"
597{-# INLINE readInstanceList #-}
598readInstanceList :: P.String -> [Int]
599readInstanceList str =
603 _ -> P.error "readInstance: no parse"
605{-# INLINE pureFoldl' #-}
606pureFoldl' :: Stream Identity Int -> Int
607pureFoldl' = runIdentity . S.foldl' (+) 0
609{-# INLINE foldableFoldl' #-}
610foldableFoldl' :: Stream Identity Int -> Int
611foldableFoldl' = F.foldl' (+) 0
613{-# INLINE foldableSum #-}
614foldableSum :: Stream Identity Int -> Int
617{-# INLINE traversableMapM #-}
618traversableMapM :: Stream Identity Int -> IO (Stream Identity Int)
619traversableMapM = 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