-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
337 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters