on 294aa21 help sign in
126 lines 4.1 kB history raw
1-- |
2-- Module : NestedUnfoldOps
3-- Copyright : (c) 2019 Composewell Technologies
4--
5-- License : BSD3
6-- Maintainer : streamly@composewell.com
8module Streamly.Benchmark.Data.NestedUnfoldOps where
10import Control.Monad.IO.Class (MonadIO (..))
11import Streamly.Internal.Data.Unfold (Unfold)
13import qualified Streamly.Internal.Data.Unfold as UF
14import qualified Streamly.Internal.Data.Fold as FL
16-- n * (n + 1) / 2 == linearCount
17concatCount :: Int -> Int
18concatCount linearCount =
19 round (((1 + 8 * fromIntegral linearCount)**(1/2::Double) - 1) / 2)
21-- double nested loop
22nestedCount2 :: Int -> Int
23nestedCount2 linearCount = round (fromIntegral linearCount**(1/2::Double))
25-- triple nested loop
26nestedCount3 :: Int -> Int
27nestedCount3 linearCount = round (fromIntegral linearCount**(1/3::Double))
29-------------------------------------------------------------------------------
30-- Stream generation and elimination
31-------------------------------------------------------------------------------
33-- generate numbers up to the argument value
34{-# INLINE source #-}
35source :: Monad m => Int -> Unfold m Int Int
36source n = UF.enumerateFromToIntegral n
38-------------------------------------------------------------------------------
39-- Benchmark ops
40-------------------------------------------------------------------------------
42{-# INLINE toNull #-}
43toNull :: MonadIO m => Int -> Int -> m ()
44toNull linearCount start = do
45 let end = start + nestedCount2 linearCount
46 UF.fold
47 (UF.map (\(x, y) -> x + y)
48 $ UF.outerProduct (source end) (source end))
49 FL.drain (start, start)
51{-# INLINE toNull3 #-}
52toNull3 :: MonadIO m => Int -> Int -> m ()
53toNull3 linearCount start = do
54 let end = start + nestedCount3 linearCount
55 UF.fold
56 (UF.map (\(x, y) -> x + y)
57 $ UF.outerProduct (source end)
58 ((UF.map (\(x, y) -> x + y)
59 $ UF.outerProduct (source end) (source end))))
60 FL.drain (start, (start, start))
62{-# INLINE concat #-}
63concat :: MonadIO m => Int -> Int -> m ()
64concat linearCount start = do
65 let end = start + concatCount linearCount
66 UF.fold
67 (UF.concat (source end) (source end))
68 FL.drain start
70{-# INLINE toList #-}
71toList :: MonadIO m => Int -> Int -> m [Int]
72toList linearCount start = do
73 let end = start + nestedCount2 linearCount
74 UF.fold
75 (UF.map (\(x, y) -> x + y)
76 $ UF.outerProduct (source end) (source end))
77 FL.toList (start, start)
79{-# INLINE toListSome #-}
80toListSome :: MonadIO m => Int -> Int -> m [Int]
81toListSome linearCount start = do
82 let end = start + nestedCount2 linearCount
83 UF.fold
84 (UF.take 1000 $ (UF.map (\(x, y) -> x + y)
85 $ UF.outerProduct (source end) (source end)))
86 FL.toList (start, start)
88{-# INLINE filterAllOut #-}
89filterAllOut :: MonadIO m => Int -> Int -> m ()
90filterAllOut linearCount start = do
91 let end = start + nestedCount2 linearCount
92 UF.fold
93 (UF.filter (< 0)
94 $ UF.map (\(x, y) -> x + y)
95 $ UF.outerProduct (source end) (source end))
96 FL.drain (start, start)
98{-# INLINE filterAllIn #-}
99filterAllIn :: MonadIO m => Int -> Int -> m ()
100filterAllIn linearCount start = do
101 let end = start + nestedCount2 linearCount
102 UF.fold
103 (UF.filter (> 0)
104 $ UF.map (\(x, y) -> x + y)
105 $ UF.outerProduct (source end) (source end))
106 FL.drain (start, start)
108{-# INLINE filterSome #-}
109filterSome :: MonadIO m => Int -> Int -> m ()
110filterSome linearCount start = do
111 let end = start + nestedCount2 linearCount
112 UF.fold
113 (UF.filter (> 1100000)
114 $ UF.map (\(x, y) -> x + y)
115 $ UF.outerProduct (source end) (source end))
116 FL.drain (start, start)
118{-# INLINE breakAfterSome #-}
119breakAfterSome :: MonadIO m => Int -> Int -> m ()
120breakAfterSome linearCount start = do
121 let end = start + nestedCount2 linearCount
122 UF.fold
123 (UF.takeWhile (<= 1100000)
124 $ UF.map (\(x, y) -> x + y)
125 $ UF.outerProduct (source end) (source end))
126 FL.drain (start, start)