Skip to content

Commit 95776ea

Browse files
committed
Add benchmark for Quicksort
1 parent bbc80dd commit 95776ea

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

bench/Data/Mutable/Quicksort.hs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{-# LANGUAGE NumericUnderscores #-}
2+
3+
module Data.Mutable.Quicksort (benchmarks) where
4+
5+
import Control.DeepSeq (force)
6+
import Control.Exception (evaluate)
7+
import Data.List (sort)
8+
import Simple.Quicksort (quickSort)
9+
import System.Random
10+
import Test.Tasty.Bench
11+
12+
-- Follows thread from https://discourse.haskell.org/t/linear-haskell-quicksort-performance/10280
13+
14+
qs :: (Ord a) => [a] -> [a]
15+
qs [] = []
16+
qs (x : xs) = qs ltx ++ x : qs gex
17+
where
18+
ltx = [y | y <- xs, y < x]
19+
gex = [y | y <- xs, y >= x]
20+
21+
linArrayQuicksort, lazyListQuicksort, stdLibSort :: [Int] -> [Int]
22+
linArrayQuicksort = quickSort
23+
lazyListQuicksort = qs
24+
stdLibSort = sort
25+
26+
gen :: StdGen
27+
gen = mkStdGen 4541645642
28+
29+
randomListBuilder :: Int -> IO [Int]
30+
randomListBuilder size = evaluate $ force $ take size (randoms gen :: [Int])
31+
32+
sizes :: [Int]
33+
sizes = [1_000, 50_000, 1_000_000]
34+
35+
benchmarks :: Benchmark
36+
benchmarks =
37+
bgroup
38+
"quicksort"
39+
( ( \size ->
40+
env (randomListBuilder size) $ \randomList ->
41+
bgroup
42+
("size " ++ (show size))
43+
[ bench "linArrayQuicksort" $
44+
nf linArrayQuicksort randomList,
45+
bench "lazyListQuicksort" $
46+
nf lazyListQuicksort randomList,
47+
bench "stdLibSort" $
48+
nf stdLibSort randomList
49+
]
50+
)
51+
<$> sizes
52+
)

bench/Main.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ module Main where
22

33
import qualified Data.Mutable.Array as Array
44
import qualified Data.Mutable.HashMap as HashMap
5+
import qualified Data.Mutable.Quicksort as Quicksort
56
import Test.Tasty.Bench (defaultMain)
67

78
main :: IO ()
89
main = do
910
defaultMain
1011
[ Array.benchmarks,
11-
HashMap.benchmarks
12+
HashMap.benchmarks,
13+
Quicksort.benchmarks
1214
]

examples/Simple/Quicksort.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{-# LANGUAGE LinearTypes #-}
22
{-# LANGUAGE NoImplicitPrelude #-}
33

4+
-- Uncomment the line below to observe the generated (optimised) Core. It will
5+
-- land in a file named “Quicksort.dump-simpl”
6+
-- {-# OPTIONS_GHC -ddump-simpl -ddump-to-file -dsuppress-all -dsuppress-uniques #-}
7+
48
-- | This module implements quicksort with mutable arrays from linear-base
59
module Simple.Quicksort where
610

linear-base.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ benchmark bench
219219
other-modules:
220220
Data.Mutable.HashMap
221221
Data.Mutable.Array
222+
Data.Mutable.Quicksort
222223
default-language: Haskell2010
223224
build-depends:
224225
base,

0 commit comments

Comments
 (0)