1
1
{-# LANGUAGE BangPatterns #-}
2
- {-# LANGUAGE TypeApplications #-}
3
2
{-# LANGUAGE LinearTypes #-}
4
3
{-# LANGUAGE NumericUnderscores #-}
4
+ {-# LANGUAGE TypeApplications #-}
5
5
6
6
module Data.Mutable.Array (benchmarks ) where
7
7
8
- import Gauge
9
- import Data.Function ((&) )
10
- import qualified Data.Unrestricted.Linear as Linear
11
- import qualified Prelude.Linear as Linear
12
8
import Control.DeepSeq (rnf )
13
-
14
9
import qualified Data.Array.Mutable.Linear as Array.Linear
10
+ import Data.Function ((&) )
11
+ import qualified Data.Unrestricted.Linear as Linear
15
12
import qualified Data.Vector
13
+ import Gauge
14
+ import qualified Prelude.Linear as Linear
16
15
17
16
dontFuse :: a -> a
18
17
dontFuse a = a
@@ -22,99 +21,103 @@ arr_size :: Int
22
21
arr_size = 10_000_000
23
22
24
23
benchmarks :: Benchmark
25
- benchmarks = bgroup " arrays"
26
- [ runImpls " toList" bToList arr_size
27
- , runImpls " map" bMap arr_size
28
- , runImpls " reads" bReads arr_size
29
- ]
24
+ benchmarks =
25
+ bgroup
26
+ " arrays"
27
+ [ runImpls " toList" bToList arr_size,
28
+ runImpls " map" bMap arr_size,
29
+ runImpls " reads" bReads arr_size
30
+ ]
30
31
31
32
--------------------------------------------------------------------------------
32
33
33
- data Impls =
34
- Impls
35
- (Array.Linear. Array Int % 1 -> () )
36
- (Data.Vector. Vector Int -> () )
34
+ data Impls
35
+ = Impls
36
+ (Array.Linear. Array Int % 1 -> () )
37
+ (Data.Vector. Vector Int -> () )
37
38
38
39
runImpls :: String -> Impls -> Int -> Benchmark
39
40
runImpls name impls size =
40
41
let Impls linear dataVector = impls
41
- in bgroup name
42
- [ bench " Data.Array.Mutable.Linear" $ whnf (runLinear linear) size
43
- , bench " Data.Vector" $ whnf (runDataVector dataVector) size
44
- ]
45
- where
46
- runLinear :: (Array.Linear. Array Int % 1 -> () ) -> Int -> ()
47
- runLinear cb sz = Linear. unur (Array.Linear. alloc sz 0 (\ a -> Linear. move (cb a)))
42
+ in bgroup
43
+ name
44
+ [ bench " Data.Array.Mutable.Linear" $ whnf (runLinear linear) size,
45
+ bench " Data.Vector" $ whnf (runDataVector dataVector) size
46
+ ]
47
+ where
48
+ runLinear :: (Array.Linear. Array Int % 1 -> () ) -> Int -> ()
49
+ runLinear cb sz = Linear. unur (Array.Linear. alloc sz 0 (\ a -> Linear. move (cb a)))
48
50
49
- runDataVector :: (Data.Vector. Vector Int -> () ) -> Int -> ()
50
- runDataVector cb sz = cb (Data.Vector. replicate sz 0 )
51
+ runDataVector :: (Data.Vector. Vector Int -> () ) -> Int -> ()
52
+ runDataVector cb sz = cb (Data.Vector. replicate sz 0 )
51
53
52
54
--------------------------------------------------------------------------------
53
55
54
56
bToList :: Impls
55
57
bToList = Impls linear dataVector
56
58
where
57
- linear :: Array.Linear. Array Int % 1 -> ()
58
- linear hm =
59
- hm
60
- Linear. & Array.Linear. toList
61
- Linear. & Linear. lift rnf
62
- Linear. & Linear. unur
63
-
64
- dataVector :: Data.Vector. Vector Int -> ()
65
- dataVector hm =
66
- hm
67
- & Data.Vector. toList
68
- & rnf
59
+ linear :: Array.Linear. Array Int % 1 -> ()
60
+ linear hm =
61
+ hm
62
+ Linear. & Array.Linear. toList
63
+ Linear. & Linear. lift rnf
64
+ Linear. & Linear. unur
65
+
66
+ dataVector :: Data.Vector. Vector Int -> ()
67
+ dataVector hm =
68
+ hm
69
+ & Data.Vector. toList
70
+ & rnf
69
71
{-# NOINLINE bToList #-}
70
72
71
73
bMap :: Impls
72
74
bMap = Impls linear dataVector
73
75
where
74
- linear :: Array.Linear. Array Int % 1 -> ()
75
- linear hm =
76
- hm
77
- Linear. & Array.Linear. map (+ 1 )
78
- Linear. & Array.Linear. unsafeGet 5
79
- Linear. & (`Linear.lseq` () )
80
-
81
- dataVector :: Data.Vector. Vector Int -> ()
82
- dataVector hm =
83
- hm
84
- & Data.Vector. map (+ 1 )
85
- & dontFuse -- This looks like cheating, I know. But we're trying to measure
86
- -- the speed of `map`, and without this, `vector` fuses the `map`
87
- -- with the subsequent `index` to skip writing to the rest of the
88
- -- vector.
89
- & (`Data.Vector.unsafeIndex` 5 )
90
- & (`seq` () )
76
+ linear :: Array.Linear. Array Int % 1 -> ()
77
+ linear hm =
78
+ hm
79
+ Linear. & Array.Linear. map (+ 1 )
80
+ Linear. & Array.Linear. unsafeGet 5
81
+ Linear. & (`Linear.lseq` () )
82
+
83
+ dataVector :: Data.Vector. Vector Int -> ()
84
+ dataVector hm =
85
+ hm
86
+ & Data.Vector. map (+ 1 )
87
+ & dontFuse -- This looks like cheating, I know. But we're trying to measure
88
+ -- the speed of `map`, and without this, `vector` fuses the `map`
89
+ -- with the subsequent `index` to skip writing to the rest of the
90
+ -- vector.
91
+ & (`Data.Vector.unsafeIndex` 5 )
92
+ & (`seq` () )
91
93
{-# NOINLINE bMap #-}
92
94
93
95
bReads :: Impls
94
96
bReads = Impls linear dataVector
95
97
where
96
- linear :: Array.Linear. Array Int % 1 -> ()
97
- linear hm =
98
- hm
99
- Linear. & Array.Linear. size
100
- Linear. & \ (Linear. Ur sz, arr) -> arr
101
- Linear. & go 0 sz
102
- where
103
- go :: Int -> Int -> Array.Linear. Array Int % 1 -> ()
104
- go start end arr
105
- | start < end =
106
- Array.Linear. unsafeGet start arr
107
- Linear. & \ (Linear. Ur i, arr') -> i `Linear.seq` go (start + 1 ) end arr'
108
- | otherwise = arr `Linear.lseq` ()
109
-
110
- dataVector :: Data.Vector. Vector Int -> ()
111
- dataVector v =
112
- let sz = Data.Vector. length v
113
- in go 0 sz
114
- where
115
- go :: Int -> Int -> ()
116
- go start end
117
- | start < end =
118
- (v Data.Vector. ! start) `seq` go (start + 1 ) end
119
- | otherwise = ()
98
+ linear :: Array.Linear. Array Int % 1 -> ()
99
+ linear hm =
100
+ hm
101
+ Linear. & Array.Linear. size
102
+ Linear. & \ (Linear. Ur sz, arr) ->
103
+ arr
104
+ Linear. & go 0 sz
105
+ where
106
+ go :: Int -> Int -> Array.Linear. Array Int % 1 -> ()
107
+ go start end arr
108
+ | start < end =
109
+ Array.Linear. unsafeGet start arr
110
+ Linear. & \ (Linear. Ur i, arr') -> i `Linear.seq` go (start + 1 ) end arr'
111
+ | otherwise = arr `Linear.lseq` ()
112
+
113
+ dataVector :: Data.Vector. Vector Int -> ()
114
+ dataVector v =
115
+ let sz = Data.Vector. length v
116
+ in go 0 sz
117
+ where
118
+ go :: Int -> Int -> ()
119
+ go start end
120
+ | start < end =
121
+ (v Data.Vector. ! start) `seq` go (start + 1 ) end
122
+ | otherwise = ()
120
123
{-# NOINLINE bReads #-}
0 commit comments