on f8afdaf help sign in
86 lines 2.6 kB history raw
1-- |
2-- Module : Streamly.Benchmark.Data.Array.Foreign.Mut
3-- Copyright : (c) 2021 Composewell Technologies
4-- License : BSD-3-Clause
5-- Maintainer : streamly@composewell.com
7{-# LANGUAGE CPP #-}
8{-# LANGUAGE FlexibleContexts #-}
9{-# LANGUAGE ScopedTypeVariables #-}
11#ifdef __HADDOCK_VERSION__
12#undef INSPECTION
13#endif
15#ifdef INSPECTION
16{-# LANGUAGE TemplateHaskell #-}
17{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-}
18#endif
20module Main (main) where
22import Control.DeepSeq (NFData(..))
23import Streamly.Prelude (MonadAsync, SerialT, IsStream)
24import System.Random (randomRIO)
26import qualified Streamly.Prelude as Stream
27import qualified Streamly.Internal.Data.Array.Foreign.Mut as MArray
29import Gauge hiding (env)
30import Streamly.Benchmark.Common
32-------------------------------------------------------------------------------
33-- Utilities
34-------------------------------------------------------------------------------
36{-# INLINE sourceUnfoldrM #-}
37sourceUnfoldrM :: (IsStream t, MonadAsync m) => Int -> Int -> t m Int
38sourceUnfoldrM value n = Stream.unfoldrM step n
39 where
40 step cnt =
41 if cnt > n + value
42 then return Nothing
43 else return (Just (cnt, cnt + 1))
45{-# INLINE benchIO #-}
46benchIO
47 :: NFData b
48 => String -> (Int -> a) -> (a -> IO b) -> Benchmark
49benchIO name src sink =
50 bench name $ nfIO $ randomRIO (1,1) >>= sink . src
52o_1_space_serial_marray :: Int -> MArray.Array Int -> [Benchmark]
53o_1_space_serial_marray value array =
54 [ benchIO "partitionBy (< 0)" (const array)
55 $ MArray.partitionBy (< 0)
56 , benchIO "partitionBy (> 0)" (const array)
57 $ MArray.partitionBy (> 0)
58 , benchIO "partitionBy (< value/2)" (const array)
59 $ MArray.partitionBy (< (value `div` 2))
60 , benchIO "partitionBy (> value/2)" (const array)
61 $ MArray.partitionBy (> (value `div` 2))
62 , benchIO "strip (< value/2 || > value/2)" (const array)
63 $ MArray.strip (\x -> x < value `div` 2 || x > value `div` 2)
64 , benchIO "strip (> 0)" (const array)
65 $ MArray.strip (> 0)
66 ]
68-------------------------------------------------------------------------------
69-- Driver
70-------------------------------------------------------------------------------
72moduleName :: String
73moduleName = "Data.Array.Foreign.Mut"
75main :: IO ()
76main = do
77 runWithCLIOptsEnv defaultStreamSize alloc allBenchmarks
79 where
81 alloc value = MArray.fromStream (sourceUnfoldrM value 0 :: SerialT IO Int)
83 allBenchmarks array value =
84 [ bgroup (o_1_space_prefix moduleName) $
85 o_1_space_serial_marray value array
86 ]