on master help sign in
Add benchmarks for *>, <*, split_, sequence_
commitaa25b34db0c837d70e74dbc7fa46c7d47da40852 authorHarendra Kumar <harendra@composewell.com> dateSat 25 Apr 2020, 08:50 (6 years ago) parent2c9feb4
1 file changed+43−14
benchmark/Streamly/Benchmark/Data/Parser.hs+43−14
benchmark/Streamly/Benchmark/Data/Parser.hs+43−14
@@ -18,7 +18,8 @@ import Control.Monad.Catch (MonadCatch)
1818import Data.Foldable (asum)
1919import Data.Monoid (Sum(..))
2020import System.Random (randomRIO)
21−import Prelude hiding (any, all, take, sequence, sequenceA, takeWhile)
21+import Prelude
22+ hiding (any, all, take, sequence, sequence_, sequenceA, takeWhile)
2223
2324import qualified Data.Traversable as TR
2425import qualified Data.Foldable as F
@@ -101,18 +102,36 @@ manyTill :: MonadCatch m => Int -> SerialT m Int -> m Int
101102manyTill value =
102103 IP.parse (PR.manyTill FL.length (PR.satisfy (> 0)) (PR.satisfy (== value)))
103104
104−{-# INLINE splitAllAny #-}
105−splitAllAny :: MonadCatch m
105+{-# INLINE splitAp #-}
106+splitAp :: MonadCatch m
106107 => Int -> SerialT m Int -> m (Bool, Bool)
107−splitAllAny value =
108+splitAp value =
108109 IP.parse ((,) <$> PR.all (<= (value `div` 2)) <*> PR.any (> value))
109110
110−{-# INLINE splitWithAllAny #-}
111−splitWithAllAny :: MonadCatch m
111+{-# INLINE splitApBefore #-}
112+splitApBefore :: MonadCatch m
113+ => Int -> SerialT m Int -> m Bool
114+splitApBefore value =
115+ IP.parse (PR.all (<= (value `div` 2)) *> PR.any (> value))
116+
117+{-# INLINE splitApAfter #-}
118+splitApAfter :: MonadCatch m
119+ => Int -> SerialT m Int -> m Bool
120+splitApAfter value =
121+ IP.parse (PR.all (<= (value `div` 2)) <* PR.any (> value))
122+
123+{-# INLINE splitWith #-}
124+splitWith :: MonadCatch m
112125 => Int -> SerialT m Int -> m (Bool, Bool)
113−splitWithAllAny value =
126+splitWith value =
114127 IP.parse (PR.splitWith (,) (PR.all (<= (value `div` 2))) (PR.any (> value)))
115128
129+{-# INLINE split_ #-}
130+split_ :: MonadCatch m
131+ => Int -> SerialT m Int -> m Bool
132+split_ value =
133+ IP.parse (PR.split_ (PR.all (<= (value `div` 2))) (PR.any (> value)))
134+
116135{-# INLINE teeAllAny #-}
117136teeAllAny :: (MonadCatch m, Ord a)
118137 => a -> SerialT m a -> m (Bool, Bool)
@@ -167,6 +186,11 @@ sequence value xs = do
167186 x <- IP.parse (TR.sequence (replicate value (PR.satisfy (> 0)))) xs
168187 return $ length x
169188
189+{-# INLINE sequence_ #-}
190+sequence_ :: MonadCatch m => Int -> SerialT m Int -> m ()
191+sequence_ value =
192+ IP.parse (F.sequence_ $ replicate value (PR.satisfy (> 0)))
193+
170194{-# INLINE choice #-}
171195choice :: MonadCatch m => Int -> SerialT m Int -> m Int
172196choice value =
@@ -203,15 +227,17 @@ o_1_space_serial_parse value =
203227 , benchIOSink value "all" $ all value
204228 , benchIOSink value "take" $ take value
205229 , benchIOSink value "takeWhile" $ takeWhile value
206− , benchIOSink value "split (all,any)" $ splitAllAny value
207− , benchIOSink value "splitWith (all,any)" $ splitWithAllAny value
230+ , benchIOSink value "splitAp" $ splitAp value
231+ , benchIOSink value "splitApBefore" $ splitApBefore value
232+ , benchIOSink value "splitApAfter" $ splitApAfter value
233+ , benchIOSink value "splitWith" $ splitWith value
208234 , benchIOSink value "many" many
209235 , benchIOSink value "some" some
210236 , benchIOSink value "manyTill" $ manyTill value
211− , benchIOSink value "tee (all,any)" $ teeAllAny value
212− , benchIOSink value "teeFst (all,any)" $ teeFstAllAny value
213− , benchIOSink value "shortest (all,any)" $ shortestAllAny value
214− , benchIOSink value "longest (all,any)" $ longestAllAny value
237+ , benchIOSink value "tee" $ teeAllAny value
238+ , benchIOSink value "teeFst" $ teeFstAllAny value
239+ , benchIOSink value "shortest" $ shortestAllAny value
240+ , benchIOSink value "longest" $ longestAllAny value
215241 , benchIOSink value "parseMany" parseMany
216242 , benchIOSink value "parseIterate" parseIterate
217243 ]
@@ -227,8 +253,11 @@ o_n_heap_serial_parse value =
227253 , benchIOSink value "sequenceA" $ sequenceA value
228254
229255 -- XXX why should this take O(n) heap, it discards the results?
256+ , benchIOSink value "sequence_" $ sequence_ value
230257 , benchIOSink value "sequenceA_" $ sequenceA_ value
231− -- XXX why O(n)?
258+ -- non-linear time complexity (parserD)
259+ , benchIOSink value "split_" $ split_ value
260+ -- XXX why O(n) heap?
232261 , benchIOSink value "choice" $ choice value
233262
234263 -- These show non-linear time complexity.