2-- Module : Streamly.Benchmark.Data.Stream.StreamK
3-- Copyright : (c) 2018 Composewell Technologies
6-- Maintainer : streamly@composewell.com
9{-# LANGUAGE FlexibleContexts #-}
10{-# LANGUAGE ScopedTypeVariables #-}
12#ifdef __HADDOCK_VERSION__
17{-# LANGUAGE TemplateHaskell #-}
18{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-}
21module Main (main) where
23import Control.Monad (when)
24import Data.Maybe (isJust)
25import System.Random (randomRIO)
27 ( tail, mapM_, foldl, last, map, mapM, concatMap, zipWith, init, iterate
31import qualified Prelude as P
32import qualified Data.List as List
34import qualified Streamly.Internal.Data.Stream.StreamK as S
35import qualified Streamly.Internal.Data.SVar as S
37import Gauge (bench, nfIO, bgroup, Benchmark, defaultMain)
39import Streamly.Benchmark.Common
47Benchmarks that need to be added
106-- toStreamK (Probably can be skipped)
113-------------------------------------------------------------------------------
114-- Stream generation and elimination
115-------------------------------------------------------------------------------
117type Stream m a = S.Stream m a
119{-# INLINE unfoldr #-}
120unfoldr :: Int -> Int -> Stream m Int
121unfoldr streamLen n = S.unfoldr step n
124 if cnt > n + streamLen
126 else Just (cnt, cnt + 1)
128{-# INLINE unfoldrM #-}
129unfoldrM :: S.MonadAsync m => Int -> Int -> Stream m Int
130unfoldrM streamLen n = S.unfoldrM step n
133 if cnt > n + streamLen
135 else return (Just (cnt, cnt + 1))
138repeat :: Int -> Int -> Stream m Int
139repeat streamLen = S.take streamLen . S.repeat
141{-# INLINE repeatM #-}
142repeatM :: S.MonadAsync m => Int -> Int -> Stream m Int
143repeatM streamLen = S.take streamLen . S.repeatM . return
145{-# INLINE replicate #-}
146replicate :: Int -> Int -> Stream m Int
147replicate = S.replicate
149{-# INLINE replicateM #-}
150replicateM :: S.MonadAsync m => Int -> Int -> Stream m Int
151replicateM streamLen = S.replicateM streamLen . return
153{-# INLINE iterate #-}
154iterate :: Int -> Int -> Stream m Int
155iterate streamLen = S.take streamLen . S.iterate (+1)
157{-# INLINE iterateM #-}
158iterateM :: S.MonadAsync m => Int -> Int -> Stream m Int
159iterateM streamLen = S.take streamLen . S.iterateM (return . (+1)) . return
161{-# INLINE fromFoldable #-}
162fromFoldable :: Int -> Int -> Stream m Int
163fromFoldable streamLen n = S.fromFoldable [n..n+streamLen]
165{-# INLINE fromFoldableM #-}
166fromFoldableM :: S.MonadAsync m => Int -> Int -> Stream m Int
167fromFoldableM streamLen n =
168 Prelude.foldr S.consM S.nil (Prelude.fmap return [n..n+streamLen])
170{-# INLINABLE concatMapFoldableWith #-}
171concatMapFoldableWith :: (S.IsStream t, Foldable f)
172 => (t m b -> t m b -> t m b) -> (a -> t m b) -> f a -> t m b
173concatMapFoldableWith f g = Prelude.foldr (f . g) S.nil
175{-# INLINE concatMapFoldableSerial #-}
176concatMapFoldableSerial :: Int -> Int -> Stream m Int
177concatMapFoldableSerial streamLen n = concatMapFoldableWith S.serial S.fromPure [n..n+streamLen]
179{-# INLINE concatMapFoldableSerialM #-}
180concatMapFoldableSerialM :: Monad m => Int -> Int -> Stream m Int
181concatMapFoldableSerialM streamLen n =
182 concatMapFoldableWith S.serial (S.fromEffect . return) [n..n+streamLen]
184-------------------------------------------------------------------------------
186-------------------------------------------------------------------------------
189drain :: Monad m => Stream m a -> m ()
193mapM_ :: Monad m => Stream m a -> m ()
194mapM_ = S.mapM_ (\_ -> return ())
197uncons :: Monad m => Stream m Int -> m ()
202 Just (_, t) -> uncons t
205init :: (Monad m, S.IsStream t) => t m a -> m ()
211tail :: (Monad m, S.IsStream t) => t m a -> m ()
212tail s = S.tail s >>= P.mapM_ tail
214{-# INLINE nullTail #-}
215nullTail :: Monad m => Stream m Int -> m ()
218 when (not r) $ S.tail s >>= P.mapM_ nullTail
220{-# INLINE headTail #-}
221headTail :: Monad m => Stream m Int -> m ()
224 when (isJust h) $ S.tail s >>= P.mapM_ headTail
227toList :: Monad m => Stream m Int -> m [Int]
231foldl' :: Monad m => Stream m Int -> m Int
232foldl' = S.foldl' (+) 0
235last :: Monad m => Stream m Int -> m (Maybe Int)
238-------------------------------------------------------------------------------
240-------------------------------------------------------------------------------
242{-# INLINE composeN #-}
245 => Int -> (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
250 3 -> drain . f . f . f
251 4 -> drain . f . f . f . f
255scanl' :: Monad m => Int -> Stream m Int -> m ()
256scanl' n = composeN n $ S.scanl' (+) 0
259map :: Monad m => Int -> Stream m Int -> m ()
260map n = composeN n $ S.map (+ 1)
263fmapK :: Monad m => Int -> Stream m Int -> m ()
264fmapK n = composeN n $ P.fmap (+ 1)
267mapM :: S.MonadAsync m => Int -> Stream m Int -> m ()
268mapM n = composeN n $ S.mapM return
270{-# INLINE mapMSerial #-}
271mapMSerial :: S.MonadAsync m => Int -> Stream m Int -> m ()
272mapMSerial n = composeN n $ S.mapMSerial return
274{-# INLINE filterEven #-}
275filterEven :: Monad m => Int -> Stream m Int -> m ()
276filterEven n = composeN n $ S.filter even
278{-# INLINE filterAllOut #-}
279filterAllOut :: Monad m => Int -> Int -> Stream m Int -> m ()
280filterAllOut streamLen n = composeN n $ S.filter (> streamLen)
282{-# INLINE filterAllIn #-}
283filterAllIn :: Monad m => Int -> Int -> Stream m Int -> m ()
284filterAllIn streamLen n = composeN n $ S.filter (<= streamLen)
286{-# INLINE _takeOne #-}
287_takeOne :: Monad m => Int -> Stream m Int -> m ()
288_takeOne n = composeN n $ S.take 1
290{-# INLINE takeAll #-}
291takeAll :: Monad m => Int -> Int -> Stream m Int -> m ()
292takeAll streamLen n = composeN n $ S.take streamLen
294{-# INLINE takeWhileTrue #-}
295takeWhileTrue :: Monad m => Int -> Int -> Stream m Int -> m ()
296takeWhileTrue streamLen n = composeN n $ S.takeWhile (<= streamLen)
298{-# INLINE dropOne #-}
299dropOne :: Monad m => Int -> Stream m Int -> m ()
300dropOne n = composeN n $ S.drop 1
302{-# INLINE dropAll #-}
303dropAll :: Monad m => Int -> Int -> Stream m Int -> m ()
304dropAll streamLen n = composeN n $ S.drop streamLen
306{-# INLINE dropWhileTrue #-}
307dropWhileTrue :: Monad m => Int -> Int -> Stream m Int -> m ()
308dropWhileTrue streamLen n = composeN n $ S.dropWhile (<= streamLen)
310{-# INLINE dropWhileFalse #-}
311dropWhileFalse :: Monad m => Int -> Stream m Int -> m ()
312dropWhileFalse n = composeN n $ S.dropWhile (<= 1)
315foldrS :: Monad m => Int -> Stream m Int -> m ()
316foldrS n = composeN n $ S.foldrS S.cons S.nil
319foldlS :: Monad m => Int -> Stream m Int -> m ()
320foldlS n = composeN n $ S.foldlS (flip S.cons) S.nil
322{-# INLINE intersperse #-}
323intersperse :: S.MonadAsync m => Int -> Int -> Stream m Int -> m ()
324intersperse streamLen n = composeN n $ S.intersperse streamLen
326-------------------------------------------------------------------------------
328-------------------------------------------------------------------------------
330{-# INLINE iterateSource #-}
333 => Int -> (Stream m Int -> Stream m Int) -> Int -> Int -> Stream m Int
334iterateSource iterStreamLen g i n = f i (unfoldrM iterStreamLen n)
337 f x m = g (f (x P.- 1) m)
340{-# INLINE iterateScan #-}
341iterateScan :: S.MonadAsync m => Int -> Int -> Int -> Stream m Int
342iterateScan iterStreamLen maxIters = iterateSource iterStreamLen (S.scanl' (+) 0) (maxIters `div` 10)
345{-# INLINE iterateDropWhileFalse #-}
346iterateDropWhileFalse :: S.MonadAsync m => Int -> Int -> Int -> Int -> Stream m Int
347iterateDropWhileFalse streamLen iterStreamLen maxIters =
348 iterateSource iterStreamLen (S.dropWhile (> streamLen)) (maxIters `div` 10)
350{-# INLINE iterateMapM #-}
351iterateMapM :: S.MonadAsync m => Int -> Int -> Int -> Stream m Int
352iterateMapM iterStreamLen maxIters = iterateSource iterStreamLen (S.mapM return) maxIters
354{-# INLINE iterateFilterEven #-}
355iterateFilterEven :: S.MonadAsync m => Int -> Int -> Int -> Stream m Int
356iterateFilterEven iterStreamLen maxIters = iterateSource iterStreamLen (S.filter even) maxIters
358{-# INLINE iterateTakeAll #-}
359iterateTakeAll :: S.MonadAsync m => Int -> Int -> Int -> Int -> Stream m Int
360iterateTakeAll streamLen iterStreamLen maxIters = iterateSource iterStreamLen (S.take streamLen) maxIters
362{-# INLINE iterateDropOne #-}
363iterateDropOne :: S.MonadAsync m => Int -> Int -> Int -> Stream m Int
364iterateDropOne iterStreamLen maxIters = iterateSource iterStreamLen (S.drop 1) maxIters
366{-# INLINE iterateDropWhileTrue #-}
367iterateDropWhileTrue :: S.MonadAsync m => Int -> Int -> Int -> Int -> Stream m Int
368iterateDropWhileTrue streamLen iterStreamLen maxIters = iterateSource iterStreamLen (S.dropWhile (<= streamLen)) maxIters
370-------------------------------------------------------------------------------
372-------------------------------------------------------------------------------
374{-# INLINE zipWith #-}
375zipWith :: S.MonadAsync m => Stream m Int -> m ()
376zipWith src = drain $ S.zipWith (,) src src
378-------------------------------------------------------------------------------
380-------------------------------------------------------------------------------
382{-# INLINE scanMap #-}
383scanMap :: Monad m => Int -> Stream m Int -> m ()
384scanMap n = composeN n $ S.map (subtract 1) . S.scanl' (+) 0
386{-# INLINE dropMap #-}
387dropMap :: Monad m => Int -> Stream m Int -> m ()
388dropMap n = composeN n $ S.map (subtract 1) . S.drop 1
390{-# INLINE dropScan #-}
391dropScan :: Monad m => Int -> Stream m Int -> m ()
392dropScan n = composeN n $ S.scanl' (+) 0 . S.drop 1
394{-# INLINE takeDrop #-}
395takeDrop :: Monad m => Int -> Int -> Stream m Int -> m ()
396takeDrop streamLen n = composeN n $ S.drop 1 . S.take streamLen
398{-# INLINE takeScan #-}
399takeScan :: Monad m => Int -> Int -> Stream m Int -> m ()
400takeScan streamLen n = composeN n $ S.scanl' (+) 0 . S.take streamLen
402{-# INLINE takeMap #-}
403takeMap :: Monad m => Int -> Int -> Stream m Int -> m ()
404takeMap streamLen n = composeN n $ S.map (subtract 1) . S.take streamLen
406{-# INLINE filterDrop #-}
407filterDrop :: Monad m => Int -> Int -> Stream m Int -> m ()
408filterDrop streamLen n = composeN n $ S.drop 1 . S.filter (<= streamLen)
410{-# INLINE filterTake #-}
411filterTake :: Monad m => Int -> Int -> Stream m Int -> m ()
412filterTake streamLen n = composeN n $ S.take streamLen . S.filter (<= streamLen)
414{-# INLINE filterScan #-}
415filterScan :: Monad m => Int -> Stream m Int -> m ()
416filterScan n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxBound)
418{-# INLINE filterMap #-}
419filterMap :: Monad m => Int -> Int -> Stream m Int -> m ()
420filterMap streamLen n = composeN n $ S.map (subtract 1) . S.filter (<= streamLen)
422-------------------------------------------------------------------------------
424-------------------------------------------------------------------------------
426-- concatMap unfoldrM/unfoldrM
428{-# INLINE concatMap #-}
429concatMap :: Int -> Int -> Int -> IO ()
430concatMap outer inner n =
431 S.drain $ S.concatMap
432 (\_ -> unfoldrM inner n)
436inspect $ hasNoTypeClasses 'concatMap
439-- concatMap unfoldr/unfoldr
441{-# INLINE concatMapPure #-}
442concatMapPure :: Int -> Int -> Int -> IO ()
443concatMapPure outer inner n =
444 S.drain $ S.concatMap
445 (\_ -> unfoldr inner n)
449inspect $ hasNoTypeClasses 'concatMapPure
452-- concatMap replicate/unfoldrM
454{-# INLINE concatMapRepl #-}
455concatMapRepl :: Int -> Int -> Int -> IO ()
456concatMapRepl outer inner n =
457 S.drain $ S.concatMap (S.replicate inner) (unfoldrM outer n)
460inspect $ hasNoTypeClasses 'concatMapRepl
465{-# INLINE sourceConcatMapId #-}
466sourceConcatMapId :: Monad m
467 => Int -> Int -> Stream m (Stream m Int)
468sourceConcatMapId val n =
469 S.fromFoldable $ fmap (S.fromEffect . return) [n..n+val]
471{-# INLINE concatMapBySerial #-}
472concatMapBySerial :: Int -> Int -> Int -> IO ()
473concatMapBySerial outer inner n =
474 S.drain $ S.concatMapBy S.serial
478-------------------------------------------------------------------------------
480-------------------------------------------------------------------------------
482{-# INLINE drainApplicative #-}
483drainApplicative :: Monad m => Stream m Int -> m ()
484drainApplicative s = drain $ do
487{-# INLINE drainMonad #-}
488drainMonad :: Monad m => Stream m Int -> m ()
489drainMonad s = drain $ do
494{-# INLINE drainMonad3 #-}
495drainMonad3 :: Monad m => Stream m Int -> m ()
496drainMonad3 s = drain $ do
502{-# INLINE filterAllOutMonad #-}
505 => Stream m Int -> m ()
506filterAllOutMonad str = drain $ do
514{-# INLINE filterAllInMonad #-}
517 => Stream m Int -> m ()
518filterAllInMonad str = drain $ do
526-------------------------------------------------------------------------------
527-- Nested Composition Pure lists
528-------------------------------------------------------------------------------
530-- There are several list benchmarks here for comparison with lists. It is easy
531-- and convenient to see the comparisons when they are here, otherwise we'll
532-- have to add a separate module for list benchmarks with the same names and
533-- then add a comparison in bench.sh.
535{-# INLINE unfoldrList #-}
536unfoldrList :: Int -> Int -> [Int]
537unfoldrList maxval n = List.unfoldr step n
542 else Just (cnt, cnt + 1)
544{-# INLINE toNullApNestedList #-}
545toNullApNestedList :: [Int] -> [Int]
546toNullApNestedList s = (+) <$> s <*> s
548{-# INLINE toNullNestedList #-}
549toNullNestedList :: [Int] -> [Int]
550toNullNestedList s = do
555{-# INLINE toNullNestedList3 #-}
556toNullNestedList3 :: [Int] -> [Int]
557toNullNestedList3 s = do
563{-# INLINE filterAllOutNestedList #-}
564filterAllOutNestedList :: [Int] -> [Int]
565filterAllOutNestedList str = do
573{-# INLINE filterAllInNestedList #-}
574filterAllInNestedList :: [Int] -> [Int]
575filterAllInNestedList str = do
583-------------------------------------------------------------------------------
585-------------------------------------------------------------------------------
588moduleName = "Data.Stream.StreamK"
590o_1_space_generation :: Int -> Benchmark
591o_1_space_generation streamLen =
593 [ benchFold "unfoldr" drain (unfoldr streamLen)
594 , benchFold "unfoldrM" drain (unfoldrM streamLen)
595 , benchFold "repeat" drain (repeat streamLen)
596 , benchFold "repeatM" drain (repeatM streamLen)
597 , benchFold "replicate" drain (replicate streamLen)
598 , benchFold "replicateM" drain (replicateM streamLen)
599 , benchFold "iterate" drain (iterate streamLen)
600 , benchFold "iterateM" drain (iterateM streamLen)
602 , benchFold "fromFoldable" drain (fromFoldable streamLen)
603 , benchFold "fromFoldableM" drain (fromFoldableM streamLen)
606 , benchFold "concatMapFoldableWith" drain (concatMapFoldableSerial streamLen)
607 , benchFold "concatMapFoldableWithM" drain (concatMapFoldableSerialM streamLen)
610o_1_space_elimination :: Int -> Benchmark
611o_1_space_elimination streamLen =
613 [ benchFold "toNull" drain (unfoldrM streamLen)
614 , benchFold "mapM_" mapM_ (unfoldrM streamLen)
615 , benchFold "uncons" uncons (unfoldrM streamLen)
616 , benchFold "init" init (unfoldrM streamLen)
617 , benchFold "foldl'" foldl' (unfoldrM streamLen)
618 , benchFold "last" last (unfoldrM streamLen)
621o_1_space_nested :: Int -> Benchmark
622o_1_space_nested streamLen =
624 [ benchFold "drainApplicative" drainApplicative (unfoldrM streamLen2)
625 , benchFold "drainMonad" drainMonad (unfoldrM streamLen2)
626 , benchFold "drainMonad3" drainMonad3 (unfoldrM streamLen3)
627 , benchFold "filterAllInMonad" filterAllInMonad (unfoldrM streamLen2)
628 , benchFold "filterAllOutMonad" filterAllOutMonad (unfoldrM streamLen2)
629 , benchFold "drainApplicative (pure)" drainApplicative (unfoldr streamLen2)
630 , benchFold "drainMonad (pure)" drainMonad (unfoldr streamLen2)
631 , benchFold "drainMonad3 (pure)" drainMonad3 (unfoldr streamLen3)
632 , benchFold "filterAllInMonad (pure)" filterAllInMonad (unfoldr streamLen2)
633 , benchFold "filterAllOutMonad (pure)" filterAllOutMonad (unfoldr streamLen2)
636 streamLen2 = round (P.fromIntegral streamLen**(1/2::P.Double)) -- double nested loop
637 streamLen3 = round (P.fromIntegral streamLen**(1/3::P.Double)) -- triple nested loop
639o_1_space_transformation :: Int -> Benchmark
640o_1_space_transformation streamLen =
641 bgroup "transformation"
642 [ benchFold "foldrS" (foldrS 1) (unfoldrM streamLen)
643 , benchFold "scanl'" (scanl' 1) (unfoldrM streamLen)
644 , benchFold "map" (map 1) (unfoldrM streamLen)
645 , benchFold "fmap" (fmapK 1) (unfoldrM streamLen)
646 , benchFold "mapM" (mapM 1) (unfoldrM streamLen)
647 , benchFold "mapMSerial" (mapMSerial 1) (unfoldrM streamLen)
650o_1_space_transformationX4 :: Int -> Benchmark
651o_1_space_transformationX4 streamLen =
652 bgroup "transformationX4"
653 [ benchFold "scanl'" (scanl' 4) (unfoldrM streamLen)
654 , benchFold "map" (map 4) (unfoldrM streamLen)
655 , benchFold "fmap" (fmapK 4) (unfoldrM streamLen)
656 , benchFold "mapM" (mapM 4) (unfoldrM streamLen)
657 , benchFold "mapMSerial" (mapMSerial 4) (unfoldrM streamLen)
658 -- XXX this is horribly slow
659 -- , benchFold "concatMap" (concatMap 4) (unfoldrM streamLen16)
662o_1_space_concat :: Int -> Benchmark
663o_1_space_concat streamLen =
665 [ benchIOSrc1 "concatMapPure (n of 1)"
666 (concatMapPure streamLen 1)
667 , benchIOSrc1 "concatMapPure (sqrt n of sqrt n)"
668 (concatMapPure streamLen2 streamLen2)
669 , benchIOSrc1 "concatMapPure (1 of n)"
670 (concatMapPure 1 streamLen)
672 , benchIOSrc1 "concatMap (n of 1)"
673 (concatMap streamLen 1)
674 , benchIOSrc1 "concatMap (sqrt n of sqrt n)"
675 (concatMap streamLen2 streamLen2)
676 , benchIOSrc1 "concatMap (1 of n)"
677 (concatMap 1 streamLen)
679 , benchIOSrc1 "concatMapRepl (sqrt n of sqrt n)"
680 (concatMapRepl streamLen2 streamLen2)
682 -- This is for comparison with concatMapFoldableWith
683 , benchIOSrc1 "concatMapWithId (n of 1) (fromFoldable)"
684 (S.drain . S.concatMapBy S.serial id . sourceConcatMapId streamLen)
686 , benchIOSrc1 "concatMapBy serial (n of 1)"
687 (concatMapBySerial streamLen 1)
688 , benchIOSrc1 "concatMapBy serial (sqrt n of sqrt n)"
689 (concatMapBySerial streamLen2 streamLen2)
690 , benchIOSrc1 "concatMapBy serial (1 of n)"
691 (concatMapBySerial 1 streamLen)
694 streamLen2 = round (P.fromIntegral streamLen**(1/2::P.Double)) -- double nested loop
696o_1_space_filtering :: Int -> Benchmark
697o_1_space_filtering streamLen =
699 [ benchFold "filter-even" (filterEven 1) (unfoldrM streamLen)
700 , benchFold "filter-all-out" (filterAllOut streamLen 1) (unfoldrM streamLen)
701 , benchFold "filter-all-in" (filterAllIn streamLen 1) (unfoldrM streamLen)
702 , benchFold "take-all" (takeAll streamLen 1) (unfoldrM streamLen)
703 , benchFold "takeWhile-true" (takeWhileTrue streamLen 1) (unfoldrM streamLen)
704 , benchFold "drop-one" (dropOne 1) (unfoldrM streamLen)
705 , benchFold "drop-all" (dropAll streamLen 1) (unfoldrM streamLen)
706 , benchFold "dropWhile-true" (dropWhileTrue streamLen 1) (unfoldrM streamLen)
707 , benchFold "dropWhile-false" (dropWhileFalse 1) (unfoldrM streamLen)
710o_1_space_filteringX4 :: Int -> Benchmark
711o_1_space_filteringX4 streamLen =
713 [ benchFold "filter-even" (filterEven 4) (unfoldrM streamLen)
714 , benchFold "filter-all-out" (filterAllOut streamLen 4) (unfoldrM streamLen)
715 , benchFold "filter-all-in" (filterAllIn streamLen 4) (unfoldrM streamLen)
716 , benchFold "take-all" (takeAll streamLen 4) (unfoldrM streamLen)
717 , benchFold "takeWhile-true" (takeWhileTrue streamLen 4) (unfoldrM streamLen)
718 , benchFold "drop-one" (dropOne 4) (unfoldrM streamLen)
719 , benchFold "drop-all" (dropAll streamLen 4) (unfoldrM streamLen)
720 , benchFold "dropWhile-true" (dropWhileTrue streamLen 4) (unfoldrM streamLen)
721 , benchFold "dropWhile-false" (dropWhileFalse 4) (unfoldrM streamLen)
724o_1_space_zipping :: Int -> Benchmark
725o_1_space_zipping streamLen =
727 [ benchFold "zipWith" zipWith (unfoldrM streamLen)
730o_1_space_mixed :: Int -> Benchmark
731o_1_space_mixed streamLen =
733 [ benchFold "scan-map" (scanMap 1) (unfoldrM streamLen)
734 , benchFold "drop-map" (dropMap 1) (unfoldrM streamLen)
735 , benchFold "drop-scan" (dropScan 1) (unfoldrM streamLen)
736 , benchFold "take-drop" (takeDrop streamLen 1) (unfoldrM streamLen)
737 , benchFold "take-scan" (takeScan streamLen 1) (unfoldrM streamLen)
738 , benchFold "take-map" (takeMap streamLen 1) (unfoldrM streamLen)
739 , benchFold "filter-drop" (filterDrop streamLen 1) (unfoldrM streamLen)
740 , benchFold "filter-take" (filterTake streamLen 1) (unfoldrM streamLen)
741 , benchFold "filter-scan" (filterScan 1) (unfoldrM streamLen)
742 , benchFold "filter-map" (filterMap streamLen 1) (unfoldrM streamLen)
745o_1_space_mixedX2 :: Int -> Benchmark
746o_1_space_mixedX2 streamLen =
748 [ benchFold "scan-map" (scanMap 2) (unfoldrM streamLen)
749 , benchFold "drop-map" (dropMap 2) (unfoldrM streamLen)
750 , benchFold "drop-scan" (dropScan 2) (unfoldrM streamLen)
751 , benchFold "take-drop" (takeDrop streamLen 2) (unfoldrM streamLen)
752 , benchFold "take-scan" (takeScan streamLen 2) (unfoldrM streamLen)
753 , benchFold "take-map" (takeMap streamLen 2) (unfoldrM streamLen)
754 , benchFold "filter-drop" (filterDrop streamLen 2) (unfoldrM streamLen)
755 , benchFold "filter-take" (filterTake streamLen 2) (unfoldrM streamLen)
756 , benchFold "filter-scan" (filterScan 2) (unfoldrM streamLen)
757 , benchFold "filter-map" (filterMap streamLen 2) (unfoldrM streamLen)
760o_1_space_mixedX4 :: Int -> Benchmark
761o_1_space_mixedX4 streamLen =
763 [ benchFold "scan-map" (scanMap 4) (unfoldrM streamLen)
764 , benchFold "drop-map" (dropMap 4) (unfoldrM streamLen)
765 , benchFold "drop-scan" (dropScan 4) (unfoldrM streamLen)
766 , benchFold "take-drop" (takeDrop streamLen 4) (unfoldrM streamLen)
767 , benchFold "take-scan" (takeScan streamLen 4) (unfoldrM streamLen)
768 , benchFold "take-map" (takeMap streamLen 4) (unfoldrM streamLen)
769 , benchFold "filter-drop" (filterDrop streamLen 4) (unfoldrM streamLen)
770 , benchFold "filter-take" (filterTake streamLen 4) (unfoldrM streamLen)
771 , benchFold "filter-scan" (filterScan 4) (unfoldrM streamLen)
772 , benchFold "filter-map" (filterMap streamLen 4) (unfoldrM streamLen)
775o_1_space_list :: Int -> Benchmark
776o_1_space_list streamLen =
778 [ bgroup "elimination"
779 [ benchList "last" (\xs -> [List.last xs]) (unfoldrList streamLen)
782 [ benchList "toNullAp" toNullApNestedList (unfoldrList streamLen2)
783 , benchList "toNull" toNullNestedList (unfoldrList streamLen2)
784 , benchList "toNull3" toNullNestedList3 (unfoldrList streamLen3)
785 , benchList "filterAllIn" filterAllInNestedList (unfoldrList streamLen2)
786 , benchList "filterAllOut" filterAllOutNestedList (unfoldrList streamLen2)
791 streamLen2 = round (P.fromIntegral streamLen**(1/2::P.Double)) -- double nested loop
792 streamLen3 = round (P.fromIntegral streamLen**(1/3::P.Double)) -- triple nested loop
794o_1_space :: Int -> Benchmark
796 bgroup (o_1_space_prefix moduleName)
797 [ o_1_space_generation streamLen
798 , o_1_space_elimination streamLen
799 , o_1_space_nested streamLen
800 , o_1_space_transformation streamLen
801 , o_1_space_transformationX4 streamLen
802 , o_1_space_concat streamLen
803 , o_1_space_filtering streamLen
804 , o_1_space_filteringX4 streamLen
805 , o_1_space_zipping streamLen
806 , o_1_space_mixed streamLen
807 , o_1_space_mixedX2 streamLen
808 , o_1_space_mixedX4 streamLen
809 , o_1_space_list streamLen
812o_n_heap :: Int -> Benchmark
814 bgroup (o_n_heap_prefix moduleName)
815 [ bgroup "transformation"
816 [ benchFold "foldlS" (foldlS 1) (unfoldrM streamLen)
821benchK :: P.String -> (Int -> Stream P.IO Int) -> Benchmark
822benchK name f = bench name $ nfIO $ randomRIO (1,1) >>= drain . f
824o_n_stack :: Int -> Int -> Int -> Benchmark
825o_n_stack streamLen iterStreamLen maxIters =
826 bgroup (o_n_stack_prefix moduleName)
827 [ bgroup "elimination"
828 [ benchFold "tail" tail (unfoldrM streamLen)
829 , benchFold "nullTail" nullTail (unfoldrM streamLen)
830 , benchFold "headTail" headTail (unfoldrM streamLen)
832 , bgroup "transformation"
834 -- XXX why do these need so much stack
835 benchFold "intersperse" (intersperse streamLen 1) (unfoldrM streamLen2)
836 , benchFold "interspersePure" (intersperse streamLen 1) (unfoldr streamLen2)
838 , bgroup "transformationX4"
840 benchFold "intersperse" (intersperse streamLen 4) (unfoldrM streamLen16)
843 [ benchK "mapM" (iterateMapM iterStreamLen maxIters)
844 , benchK "scan(1/10)" (iterateScan iterStreamLen maxIters)
845 , benchK "filterEven" (iterateFilterEven iterStreamLen maxIters)
846 , benchK "takeAll" (iterateTakeAll streamLen iterStreamLen maxIters)
847 , benchK "dropOne" (iterateDropOne iterStreamLen maxIters)
848 , benchK "dropWhileFalse(1/10)" (iterateDropWhileFalse streamLen iterStreamLen maxIters)
849 , benchK "dropWhileTrue" (iterateDropWhileTrue streamLen iterStreamLen maxIters)
853 streamLen2 = round (P.fromIntegral streamLen**(1/2::P.Double)) -- double nested loop
854 streamLen16 = round (P.fromIntegral streamLen**(1/16::P.Double)) -- triple nested loop
856o_n_space :: Int -> Benchmark
858 bgroup (o_n_space_prefix moduleName)
859 [ bgroup "elimination"
860 [ benchFold "toList" toList (unfoldrM streamLen)
864{-# INLINE benchList #-}
865benchList :: P.String -> ([Int] -> [Int]) -> (Int -> [Int]) -> Benchmark
866benchList name run f = bench name $ nfIO $ randomRIO (1,1) >>= return . run . f
871 [ o_1_space streamLen
872 , o_n_stack streamLen iterStreamLen maxIters
874 , o_n_space streamLen
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