/
Benchmark.hs
135 lines (91 loc) · 3.56 KB
/
Benchmark.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
{-# LANGUAGE FlexibleContexts #-}
import Criterion.Main
import Data.Vector.Generic as VG
import Data.Vector.Storable as VS
import Data.Vector.Unboxed as VU
import Data.Vec3
import qualified Data.Vec3.Unboxed.Contiguous as C
oXYZ :: (Double, Double, Double)
oXYZ = (19.899999999, 22.8, -100500)
oXYZ' :: (Double, Double, Double)
oXYZ' = (2, 12, 85.06)
n :: Int
n = 1000000
bigN :: Int
bigN = n * 10
-- Access to whole elements
testWhole :: (Vec3 a, VG.Vector v a) => v a -> v a -> Benchmarkable
testWhole v v2 = whnf (uncurry $ VG.zipWith (<+>)) (v, v2)
-- Access to x components of elements
testComp :: (Vec3 a, VG.Vector v a, VG.Vector v Double) => v a -> v a -> Benchmarkable
testComp v v' = whnf (uncurry $ VG.zipWith foo) (v, v')
where
foo v1 v2 = x1 + x2
where
(x1, _, _) = toXYZ v1
(x2, _, _) = toXYZ v2
-- zipWith using generic dot product
testDotM :: (Vec3 a, VG.Vector v a, VG.Vector v Double) => v a -> v a -> Benchmarkable
testDotM v v2 = whnf (uncurry $ VG.zipWith dotM') (v, v2)
where
o = fromXYZ oXYZ
m = fromRows (o, o, o)
dotM' e1 e2 = dotM e1 e2 m
-- Note that source arrays are not forced in test functions.
tv :: VU.Vector UVec3
tv = VG.replicate n $ fromXYZ oXYZ
cv :: VU.Vector C.UVec3
cv = VG.replicate n $ fromXYZ oXYZ
sv :: VS.Vector SVec3
sv = VG.replicate n $ fromXYZ oXYZ
tv' :: VU.Vector UVec3
tv' = VG.replicate n $ fromXYZ oXYZ'
cv' :: VU.Vector C.UVec3
cv' = VG.replicate n $ fromXYZ oXYZ'
sv' :: VS.Vector SVec3
sv' = VG.replicate n $ fromXYZ oXYZ'
bsv :: VS.Vector SVec3
bsv = VG.replicate bigN $ fromXYZ oXYZ
bcv :: VU.Vector C.UVec3
bcv = VG.replicate bigN $ fromXYZ oXYZ
btv :: VU.Vector UVec3
btv = VG.replicate bigN $ fromXYZ oXYZ
bsv' :: VS.Vector SVec3
bsv' = VG.replicate bigN $ fromXYZ oXYZ'
bcv' :: VU.Vector C.UVec3
bcv' = VG.replicate bigN $ fromXYZ oXYZ'
btv' :: VU.Vector UVec3
btv' = VG.replicate bigN $ fromXYZ oXYZ'
main :: IO ()
main = defaultMain
[ bgroup "zipWith <+>"
[ bench "SVec/Storable" $ testWhole sv sv'
, bench "UVec/Unboxed" $ testWhole tv tv'
, bench "UVec/Unboxed/Contiguous" $ testWhole cv cv'
]
, bgroup "zipWith-by-x +"
[ bench "SVec/Storable" $ testComp sv sv'
, bench "UVec/Unboxed" $ testComp tv tv'
, bench "UVec/Unboxed/Contiguous" $ testComp cv cv'
]
, bgroup "zipWith dotM"
[ bench "SVec/Storable" $ testDotM sv sv'
, bench "UVec/Unboxed" $ testDotM tv tv'
, bench "UVec/Unboxed/Contiguous" $ testDotM cv cv'
]
, bgroup "zipWith <+> 1M"
[ bench "SVec/Storable" $ testWhole bsv bsv'
, bench "UVec/Unboxed" $ testWhole btv btv'
, bench "UVec/Unboxed/Contiguous" $ testWhole bcv bcv'
]
, bgroup "zipWith-by-x + 1M"
[ bench "SVec/Storable" $ testComp bsv bsv'
, bench "UVec/Unboxed" $ testComp btv btv'
, bench "UVec/Unboxed/Contiguous" $ testComp bcv bcv'
]
, bgroup "zipWith dotM 1M"
[ bench "SVec/Storable" $ testDotM bsv bsv'
, bench "UVec/Unboxed" $ testDotM btv btv'
, bench "UVec/Unboxed/Contiguous" $ testDotM bcv bcv'
]
]