Skip to content

Commit

Permalink
[#8] WIP benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
pgujjula committed May 10, 2024
1 parent 12ed828 commit 2692c25
Show file tree
Hide file tree
Showing 3 changed files with 337 additions and 0 deletions.
1 change: 1 addition & 0 deletions apply-merge.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ benchmark apply-merge-benchmarks
Data.List.ApplyMerge
Data.List.NonEmpty.ApplyMerge
Data.PQueue.Prio.Min.Mutable
Bench.ApplyMerge
Bench.Data.DoublyLinkedList.STRef
Bench.PriorityQueue.MinPQueue
Bench.PriorityQueue.MinPQueue.Mutable
Expand Down
334 changes: 334 additions & 0 deletions bench/Bench/ApplyMerge.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,334 @@
module Bench.ApplyMerge (benchmarks) where

import ApplyMerge.DoublyLinkedList qualified
import ApplyMerge.IntMap qualified
import ApplyMerge.IntSet qualified
import ApplyMerge.MergeAll qualified
import Data.Function ((&))
import Data.List.Ordered (minus)
import Test.Tasty.Bench (Benchmark, bench, bgroup, nf)

benchmarks :: Benchmark
benchmarks =
bgroup
"applyMerge"
[ linearBenchmark,
triangularBenchmark,
skewedTriangularBenchmark,
primesBenchmark
]

linearBenchmark :: Benchmark
linearBenchmark =
bgroup
"linear shape: applyMerge const [1..] [1..]"
(map mkBench [1 .. 6])
where
mkBench :: Int -> Benchmark
mkBench i =
let limit :: Int
limit = 10 ^ i

doublyLinkedBenchmark :: Benchmark
doublyLinkedBenchmark = bench "DoublyLinkedList" (nf collapse limit)
where
collapse :: Int -> Int
collapse n =
let one = (n `quot` maxBound) + 1
in ApplyMerge.DoublyLinkedList.applyMerge const [one ..] [one ..]
& take n
& sum

intMapBenchmark :: Benchmark
intMapBenchmark = bench "IntMap" (nf collapse limit)
where
collapse :: Int -> Int
collapse n =
let one = (n `quot` maxBound) + 1
in ApplyMerge.IntMap.applyMerge const [one ..] [one ..]
& take n
& sum

intSetBenchmark :: Benchmark
intSetBenchmark = bench "IntSet" (nf collapse limit)
where
collapse :: Int -> Int
collapse n =
let one = (n `quot` maxBound) + 1
in ApplyMerge.IntSet.applyMerge const [one ..] [one ..]
& take n
& sum

mergeAllBenchmark :: Benchmark
mergeAllBenchmark = bench "MergeAll" (nf collapse limit)
where
collapse :: Int -> Int
collapse n =
let one = (n `quot` maxBound) + 1
in ApplyMerge.MergeAll.applyMerge const [one ..] [one ..]
& take n
& sum

mergeAll1Benchmark :: Benchmark
mergeAll1Benchmark = bench "MergeAll1" (nf collapse limit)
where
collapse :: Int -> Int
collapse n =
let one = (n `quot` maxBound) + 1
in ApplyMerge.MergeAll.applyMerge (const id) [one ..] [one ..]
& take n
& sum
in bgroup
(show limit)
[ doublyLinkedBenchmark,
intMapBenchmark,
intSetBenchmark,
mergeAllBenchmark,
mergeAll1Benchmark
]

triangularBenchmark :: Benchmark
triangularBenchmark =
bgroup
"triangular shape: applyMerge (+) [1..] [1..]"
(map mkBench [1 .. 6])
where
mkBench :: Int -> Benchmark
mkBench i =
let limit :: Int
limit = 10 ^ i

doublyLinkedBenchmark :: Benchmark
doublyLinkedBenchmark = bench "DoublyLinkedList" (nf collapse limit)
where
collapse :: Int -> Int
collapse n =
let one = (n `quot` maxBound) + 1
in ApplyMerge.DoublyLinkedList.applyMerge (+) [one ..] [one ..]
& take n
& sum

intMapBenchmark :: Benchmark
intMapBenchmark = bench "IntMap" (nf collapse limit)
where
collapse :: Int -> Int
collapse n =
let one = (n `quot` maxBound) + 1
in ApplyMerge.IntMap.applyMerge (+) [one ..] [one ..]
& take n
& sum

intSetBenchmark :: Benchmark
intSetBenchmark = bench "IntSet" (nf collapse limit)
where
collapse :: Int -> Int
collapse n =
let one = (n `quot` maxBound) + 1
in ApplyMerge.IntSet.applyMerge (+) [one ..] [one ..]
& take n
& sum

mergeAllBenchmark :: Benchmark
mergeAllBenchmark = bench "MergeAll" (nf collapse limit)
where
collapse :: Int -> Int
collapse n =
let one = (n `quot` maxBound) + 1
in ApplyMerge.MergeAll.applyMerge (+) [one ..] [one ..]
& take n
& sum

in bgroup
(show limit)
[ doublyLinkedBenchmark,
intMapBenchmark,
intSetBenchmark,
mergeAllBenchmark
]

skewedTriangularBenchmark :: Benchmark
skewedTriangularBenchmark =
bgroup
"skewed triangular shape: applyMerge (+) [1..] [1..]"
(map mkBench [1 .. 6])
where
mkBench :: Int -> Benchmark
mkBench i =
let limit :: Int
limit = 10 ^ i

doublyLinkedBenchmark :: Benchmark
doublyLinkedBenchmark = bench "DoublyLinkedList" (nf collapse limit)
where
collapse :: Int -> Int
collapse n =
let one = (n `quot` maxBound) + 1
in ApplyMerge.DoublyLinkedList.applyMerge (\x y -> 4 * x + y) [one ..] [one ..]
& take n
& sum

intMapBenchmark :: Benchmark
intMapBenchmark = bench "IntMap" (nf collapse limit)
where
collapse :: Int -> Int
collapse n =
let one = (n `quot` maxBound) + 1
in ApplyMerge.IntMap.applyMerge (\x y -> 4 * x + y) [one ..] [one ..]
& take n
& sum

intSetBenchmark :: Benchmark
intSetBenchmark = bench "IntSet" (nf collapse limit)
where
collapse :: Int -> Int
collapse n =
let one = (n `quot` maxBound) + 1
in ApplyMerge.IntSet.applyMerge (\x y -> 4 * x + y) [one ..] [one ..]
& take n
& sum

mergeAllBenchmark :: Benchmark
mergeAllBenchmark = bench "MergeAll" (nf collapse limit)
where
collapse :: Int -> Int
collapse n =
let one = (n `quot` maxBound) + 1
in ApplyMerge.MergeAll.applyMerge (\x y -> 4 * x + y) [one ..] [one ..]
& take n
& sum

mergeAll1Benchmark :: Benchmark
mergeAll1Benchmark = bench "MergeAll" (nf collapse limit)
where
collapse :: Int -> Int
collapse n =
let one = (n `quot` maxBound) + 1
in ApplyMerge.MergeAll.applyMerge (\x y -> x + 4 * y) [one ..] [one ..]
& take n
& sum
in bgroup
(show limit)
[ doublyLinkedBenchmark,
intMapBenchmark,
intSetBenchmark,
mergeAllBenchmark,
mergeAll1Benchmark
]

primesBenchmark :: Benchmark
primesBenchmark =
bgroup
"primes"
(map mkBench [1 .. 6])
where
mkBench :: Int -> Benchmark
mkBench i =
let limit :: Int
limit = 10 ^ i

doublyLinkedBenchmark :: Benchmark
doublyLinkedBenchmark = bench "DoublyLinkedList" (nf collapse limit)
where
collapse :: Int -> Int
collapse n =
let zero :: Int
zero = (n `quot` maxBound)

primes :: [Int]
primes = 2 : 3 : ([5 ..] `minus` composites)

composites :: [Int]
composites =
ApplyMerge.DoublyLinkedList.applyMerge
(\p j -> p * (p + j))
primes
[zero ..]
in primes
& takeWhile (<= n)
& sum

intMapBenchmark :: Benchmark
intMapBenchmark = bench "IntMap" (nf collapse limit)
where
collapse :: Int -> Int
collapse n =
let zero :: Int
zero = (n `quot` maxBound)

primes :: [Int]
primes = 2 : ([3 ..] `minus` composites)

composites :: [Int]
composites =
ApplyMerge.IntMap.applyMerge (\p j -> p * (p + j)) primes [zero ..]
in primes
& takeWhile (<= n)
& sum

intSetBenchmark :: Benchmark
intSetBenchmark = bench "IntSet" (nf collapse limit)
where
collapse :: Int -> Int
collapse n =
let zero :: Int
zero = (n `quot` maxBound)

primes :: [Int]
primes = 2 : ([3 ..] `minus` composites)

composites :: [Int]
composites =
ApplyMerge.IntSet.applyMerge (\p j -> p * (p + j)) primes [zero ..]
in primes
& takeWhile (<= n)
& sum

mergeAllBenchmark :: Benchmark
mergeAllBenchmark = bench "MergeAll" (nf collapse limit)
where
collapse :: Int -> Int
collapse n =
let zero :: Int
zero = (n `quot` maxBound)

primes :: [Int]
primes = 2 : ([3 ..] `minus` composites)

composites :: [Int]
composites =
ApplyMerge.MergeAll.applyMerge (\p j -> p * (p + j)) primes [zero ..]
in primes
& takeWhile (<= n)
& sum

mergeAll1Benchmark :: Benchmark
mergeAll1Benchmark = bench "MergeAll" (nf collapse limit)
where
collapse :: Int -> Int
collapse n =
let zero :: Int
zero = (n `quot` maxBound)

primes :: [Int]
primes = 2 : 3 : 5 : ([7 ..] `minus` composites)

composites :: [Int]
composites =
ApplyMerge.MergeAll.applyMerge (\j p -> p * (p + j)) [zero ..] primes
in primes
& takeWhile (<= n)
& sum
in bgroup
(show limit)
[ doublyLinkedBenchmark,
intMapBenchmark,
intSetBenchmark,
mergeAllBenchmark,
mergeAll1Benchmark
]

-- hyperbolicBenchmark :: Benchmark
-- hyperbolicBenchmark = undefined
--
-- skewedHyperbolicBenchmark :: Benchmark
-- skewedHyperbolicBenchmark = undefined
2 changes: 2 additions & 0 deletions bench/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Bench.PriorityQueue.MinPQueue qualified
import Bench.PriorityQueue.MinPQueue.Mutable qualified
import Data.Function ((&))
import Test.Tasty.Bench (Benchmark, bench, bgroup, defaultMain, nf)
import Bench.ApplyMerge qualified

main :: IO ()
main =
Expand All @@ -22,6 +23,7 @@ main =
benchCommon "IntMap" ApplyMerge.IntMap.applyMerge,
benchCommon "IntSet" ApplyMerge.IntSet.applyMerge,
benchCommon "MergeAll" ApplyMerge.MergeAll.applyMerge,
Bench.ApplyMerge.benchmarks,
Bench.Data.DoublyLinkedList.STRef.benchmarks,
Bench.PriorityQueue.MinPQueue.benchmarks,
Bench.PriorityQueue.MinPQueue.Mutable.benchmarks
Expand Down

0 comments on commit 2692c25

Please sign in to comment.