Skip to content

Commit c117ae3

Browse files
committed
Add RAList benchmarks
1 parent 6de3e14 commit c117ae3

File tree

8 files changed

+104
-13
lines changed

8 files changed

+104
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ dist-newstyle/
44
cabal.project.local
55
tmp/
66
*.agdai
7+
bench.html

cabal.project

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ packages: vec-optics
1212

1313
packages: tests
1414

15-
tests: true
15+
tests: True
16+
benchmarks; True
1617

1718
package dec
1819
ghc-options: -Wall

ral/bench/Bench.hs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
module Main where
2+
3+
import Criterion.Main (bench, bgroup, defaultMain, whnf)
4+
5+
import qualified Data.List as L
6+
import qualified Data.RAList as R
7+
import qualified Data.RAList.NonEmpty as NER
8+
import qualified Data.Vector as V
9+
import qualified Data.Vector.Unboxed as U
10+
11+
list :: [Int]
12+
list = [1 .. 10000]
13+
14+
ralist :: R.RAList Int
15+
ralist = R.fromList list
16+
17+
vector :: V.Vector Int
18+
vector = V.fromList list
19+
20+
uvector :: U.Vector Int
21+
uvector = U.fromList list
22+
23+
rlast :: R.RAList a -> a
24+
rlast (R.NonEmpty r) = NER.last r
25+
rlast R.Empty = error "rlast Empty"
26+
27+
main :: IO ()
28+
main = defaultMain
29+
[ bgroup "Last"
30+
[ bench "List" $ whnf L.last list
31+
, bench "RAList" $ whnf rlast ralist
32+
, bench "Vector" $ whnf V.last vector
33+
, bench "Vector.Unboxed" $ whnf U.last uvector
34+
]
35+
, bgroup "Index"
36+
[ bench "List" $ whnf (\xs -> xs L.!! (L.length xs - 1)) list
37+
, bench "RAList" $ whnf (\xs -> xs R.! (R.length xs - 1)) ralist
38+
, bench "Vector" $ whnf (\xs -> xs V.! (V.length xs - 1)) vector
39+
, bench "Vector.Unboxed" $ whnf (\xs -> xs U.! (U.length xs - 1)) uvector
40+
]
41+
, bgroup "Cons"
42+
[ bench "List" $ whnf (0 :) list
43+
, bench "RAList" $ whnf (R.cons 0) ralist
44+
, bench "Vector" $ whnf (V.cons 0) vector
45+
, bench "Vector.Unboxed" $ whnf (U.cons 0) uvector
46+
]
47+
, bgroup "Length"
48+
[ bench "List" $ whnf L.length list
49+
, bench "RAList" $ whnf R.length ralist
50+
, bench "Vector" $ whnf V.length vector
51+
, bench "Vector.Unboxed" $ whnf U.length uvector
52+
]
53+
, bgroup "LastAfterCons"
54+
[ bench "List" $ whnf (\xs -> L.last $ 0 : xs ) list
55+
, bench "RAList" $ whnf (\xs -> rlast $ R.cons 0 xs) ralist
56+
, bench "Vector" $ whnf (\xs -> V.last $ V.cons 0 xs) vector
57+
, bench "Vector.Unboxed" $ whnf (\xs -> U.last $ U.cons 0 xs) uvector
58+
]
59+
]

ral/ral.cabal

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,15 @@ library
9898
-- if impl(ghc >= 8.0)
9999
-- build-depends: dump-core
100100
-- ghc-options: -fplugin=DumpCore -fplugin-opt DumpCore:core-html
101+
102+
benchmark ral-bench
103+
type: exitcode-stdio-1.0
104+
default-language: Haskell2010
105+
hs-source-dirs: bench
106+
ghc-options: -Wall -fprint-explicit-kinds -threaded
107+
main-is: Bench.hs
108+
build-depends:
109+
, base
110+
, criterion
111+
, ral
112+
, vector

ral/src/Data/RAList.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module Data.RAList (
1515
(!),
1616
(!?),
1717
uncons,
18+
length,
19+
null,
1820
-- * Conversions
1921
toList,
2022
fromList,

ral/src/Data/RAList/Internal.hs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ module Data.RAList.Internal (
1818
-- * Indexing
1919
(!),
2020
(!?),
21+
length,
22+
null,
2123
-- * Conversions
2224
toList,
2325
fromList,
@@ -75,11 +77,8 @@ instance I.Foldable RAList where
7577
foldMap f (NonEmpty xs) = I.foldMap f xs
7678

7779
#if MIN_VERSION_base(4,8,0)
78-
length Empty = 0
79-
length (NonEmpty xs) = I.length xs
80-
81-
null Empty = True
82-
null (NonEmpty _) = False
80+
length = length
81+
null = null
8382
#endif
8483

8584
instance NFData a => NFData (RAList a) where
@@ -197,6 +196,14 @@ fromList (x:xs) = NonEmpty (NE.fromNonEmpty (x :| xs))
197196
Empty !? _ = Nothing
198197
NonEmpty xs !? i = xs NE.!? i
199198

199+
length :: RAList a -> Int
200+
length Empty = 0
201+
length (NonEmpty xs) = NE.length xs
202+
203+
null :: RAList a -> Bool
204+
null Empty = True
205+
null (NonEmpty _) = False
206+
200207
-------------------------------------------------------------------------------
201208
-- Folds
202209
-------------------------------------------------------------------------------

ral/src/Data/RAList/NonEmpty.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ module Data.RAList.NonEmpty (
1919
last,
2020
uncons,
2121
tail,
22+
length,
23+
null,
2224
-- * Conversions
2325
toNonEmpty,
2426
fromNonEmpty,

ral/src/Data/RAList/NonEmpty/Internal.hs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ module Data.RAList.NonEmpty.Internal (
2121
(!?),
2222
head,
2323
last,
24+
length,
25+
null,
2426
-- * Conversions
2527
toNonEmpty,
2628
toList,
@@ -112,13 +114,8 @@ instance I.Foldable NERAList where
112114
foldMap f (NE xs) = I.foldMap f xs
113115

114116
#if MIN_VERSION_base(4,8,0)
115-
length (NE xs) = go 0 1 xs where
116-
go :: Int -> Int -> NERAList' n a -> Int
117-
go !acc s (Last _) = acc + s
118-
go acc s (Cons0 r) = go acc (s + s) r
119-
go acc s (Cons1 _ r) = go (acc + s) (s + s) r
120-
121-
null _ = False
117+
length = length
118+
null = null
122119
#endif
123120

124121
#ifdef MIN_VERSION_semigroupoids
@@ -292,6 +289,16 @@ last' (Last t) = Tr.last t
292289
last' (Cons0 r) = last' r
293290
last' (Cons1 _ r) = last' r
294291

292+
length :: NERAList a -> Int
293+
length (NE xs) = go 0 1 xs where
294+
go :: Int -> Int -> NERAList' n a -> Int
295+
go !acc s (Last _) = acc + s
296+
go acc s (Cons0 r) = go acc (s + s) r
297+
go acc s (Cons1 _ r) = go (acc + s) (s + s) r
298+
299+
null :: NERAList a -> Bool
300+
null _ = False
301+
295302
-------------------------------------------------------------------------------
296303
-- Folds
297304
-------------------------------------------------------------------------------

0 commit comments

Comments
 (0)