-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #477 from tweag/tbagrel1/quicksort-perf-bench
Add Quicksort benchmark and export benchmark results as an artifact
- Loading branch information
Showing
8 changed files
with
110 additions
and
30 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,40 @@ | ||
{-# LANGUAGE NumericUnderscores #-} | ||
|
||
module Data.Mutable.Quicksort (benchmarks) where | ||
|
||
import Control.DeepSeq (force) | ||
import Control.Exception (evaluate) | ||
import Data.List (sort) | ||
import Simple.Quicksort (quicksortUsingArray, quicksortUsingList) | ||
import System.Random | ||
import Test.Tasty.Bench | ||
|
||
-- Follows thread from https://discourse.haskell.org/t/linear-haskell-quicksort-performance/10280 | ||
|
||
gen :: StdGen | ||
gen = mkStdGen 4541645642 | ||
|
||
randomListBuilder :: Int -> IO [Int] | ||
randomListBuilder size = evaluate $ force $ take size (randoms gen :: [Int]) | ||
|
||
sizes :: [Int] | ||
sizes = [1_000, 50_000, 1_000_000] | ||
|
||
benchmarks :: Benchmark | ||
benchmarks = | ||
bgroup | ||
"quicksort" | ||
( ( \size -> | ||
env (randomListBuilder size) $ \randomList -> | ||
bgroup | ||
("size " ++ (show size)) | ||
[ bench "quicksortUsingArray" $ | ||
nf quicksortUsingArray randomList, | ||
bench "quicksortUsingList" $ | ||
nf quicksortUsingList randomList, | ||
bench "sortStdLib" $ | ||
nf sort randomList | ||
] | ||
) | ||
<$> sizes | ||
) |
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 |
---|---|---|
@@ -1 +1,6 @@ | ||
packages: *.cabal | ||
|
||
tests: True | ||
benchmarks: True | ||
allow-newer: all | ||
index-state: 2024-09-13T13:31:57Z |
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
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 |
---|---|---|
@@ -1,19 +1,29 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Test.Simple.Quicksort (quickSortTests) where | ||
module Test.Simple.Quicksort (quicksortTests) where | ||
|
||
import Data.List (sort) | ||
import Hedgehog | ||
import qualified Hedgehog.Gen as Gen | ||
import qualified Hedgehog.Range as Range | ||
import Simple.Quicksort (quickSort) | ||
import Simple.Quicksort (quicksortUsingArray, quicksortUsingList) | ||
import Test.Tasty | ||
import Test.Tasty.Hedgehog (testPropertyNamed) | ||
|
||
quickSortTests :: TestTree | ||
quickSortTests = testPropertyNamed "quicksort sorts" "testQuicksort" testQuicksort | ||
quicksortTests :: TestTree | ||
quicksortTests = | ||
testGroup | ||
"quicksort tests" | ||
[ testPropertyNamed "sort xs === quicksortUsingArray xs" "testQuicksortUsingArray" testQuicksortUsingArray, | ||
testPropertyNamed "sort xs === quicksortUsingList xs" "testQuicksortUsingList" testQuicksortUsingList | ||
] | ||
|
||
testQuicksort :: Property | ||
testQuicksort = property $ do | ||
testQuicksortUsingArray :: Property | ||
testQuicksortUsingArray = property $ do | ||
xs <- forAll $ Gen.list (Range.linear 0 1000) (Gen.int $ Range.linear 0 100) | ||
sort xs === quickSort xs | ||
sort xs === quicksortUsingArray xs | ||
|
||
testQuicksortUsingList :: Property | ||
testQuicksortUsingList = property $ do | ||
xs <- forAll $ Gen.list (Range.linear 0 1000) (Gen.int $ Range.linear 0 100) | ||
sort xs === quicksortUsingList xs |