on 46314a2 help sign in
82 lines 2.4 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_n_space_serial_marray :: Int -> MArray.Array Int -> [Benchmark]
53o_n_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 ]
64-------------------------------------------------------------------------------
65-- Driver
66-------------------------------------------------------------------------------
68moduleName :: String
69moduleName = "Data.Array.Foreign.Mut"
71main :: IO ()
72main = do
73 runWithCLIOptsEnv defaultStreamSize alloc allBenchmarks
75 where
77 alloc value = MArray.fromStream (sourceUnfoldrM value 0 :: SerialT IO Int)
79 allBenchmarks array value =
80 [ bgroup (o_1_space_prefix moduleName) $
81 o_n_space_serial_marray value array
82 ]