Commit 733171e8 authored by Hans-Peter Deifel's avatar Hans-Peter Deifel 🐢

Add microbenchmarks for SumBag

parent 1e542693
......@@ -9,6 +9,7 @@ import qualified Data.List.BenchUtils
import qualified Copar.Algorithm.BenchInitialize
import qualified Data.BenchRefinablePartition
import qualified Data.BenchBlockQueue
import qualified Data.BenchSumBag
main :: IO ()
main = defaultMain
......@@ -19,4 +20,5 @@ main = defaultMain
, Copar.Algorithm.BenchInitialize.benchmarks
, Data.BenchRefinablePartition.benchmarks
, Data.BenchBlockQueue.benchmarks
, Data.BenchSumBag.benchmarks
]
{-# LANGUAGE BangPatterns #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Data.BenchSumBag (benchmarks) where
import Criterion
import Data.Monoid
import Data.Coerce
import Control.DeepSeq
import qualified Data.SumBag as SumBag
import Data.SumBag (SumBag)
benchmarks :: Benchmark
benchmarks = bgroup "Data.SumBag" [benchInsert, benchDelete, benchEq]
benchInsert :: Benchmark
benchInsert = bgroup "insert" $
let mkBench n = withBag (range 1 n) $ \(~bag) ->
bench (show n ++ " elements") $ whnf (SumBag.insert 0) bag
in map mkBench [1000,2000..10000]
benchDelete :: Benchmark
benchDelete = bgroup "delete" $
let mkBench n = withBag (range 1 n) $ \(~bag) ->
bench (show n ++ " elements") $ whnf (SumBag.delete 1) bag
in map mkBench [1000,2000..10000]
benchEq :: Benchmark
benchEq = bgroup "eq" $
let mkBench n =
withBag (range 1 n) $ \(~bag) ->
withBag (reverse $ range 1 n) $ \(~revbag) ->
bench (show n ++ " elements") $ whnf (uncurry (==)) (bag, revbag)
in map mkBench [1000,2000..10000]
range :: Int -> Int -> [Sum Int]
range a b = coerce [a..b]
withBag :: [Sum Int] -> (SumBag (Sum Int) -> Benchmark) -> Benchmark
withBag input = env setupEnv
where setupEnv = do
let !bag = SumBag.fromList input
return bag
instance NFData (SumBag a) where
rnf a = seq a ()
......@@ -212,6 +212,7 @@ benchmark bench
, Copar.Algorithm.BenchInitialize
, Data.BenchRefinablePartition
, Data.BenchBlockQueue
, Data.BenchSumBag
default-extensions: GADTs
, StandaloneDeriving
, DeriveFunctor
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment