@@ -5,34 +5,52 @@ import java.util.concurrent.TimeUnit.{SECONDS, MILLISECONDS}
5
5
import scala .util .Random
6
6
7
7
@ BenchmarkMode (Array (Mode .AverageTime ))
8
- @ Fork (3 )
8
+ @ Fork (2 )
9
9
@ Threads (3 )
10
10
@ Warmup (iterations = 3 , time = 3 , timeUnit = SECONDS )
11
11
@ Measurement (iterations = 5 , time = 5 , timeUnit = SECONDS )
12
12
@ OutputTimeUnit (MILLISECONDS )
13
13
@ State (Scope .Benchmark )
14
14
class InlineTraitBenchmark {
15
- // @Param(Array("100", "200", "300"))
16
15
var matrixSize : Int = 300
17
16
17
+ var numPairs : Int = 3_000_000
18
+
18
19
def intMatrixElems : List [List [Int ]] =
19
20
List .tabulate(matrixSize, matrixSize)((_, _) => Random .nextInt())
20
21
22
+ def pairElems : List [(First , Second )] = List .tabulate(numPairs)(_ % 2 match {
23
+ case 0 => (Random .nextInt(), Random .nextDouble())
24
+ case 1 => (Random .nextInt(Char .MaxValue ).asInstanceOf [Char ], Random .nextInt(Short .MaxValue ).asInstanceOf [Short ])
25
+ })
26
+
21
27
@ Param (Array (" standard" , " specialized" , " inlinetrait" ))
22
28
var libType : String = _
23
29
24
30
var m1 : BenchmarkMatrix = _
25
31
var m2 : BenchmarkMatrix = _
26
32
33
+ var pairs : List [BenchmarkPair ] = _
34
+
27
35
@ Setup (Level .Trial )
28
36
def setup = {
29
37
Random .setSeed(matrixSize)
30
38
31
39
val matrixFactory = BenchmarkMatrix .ofType(libType)
32
40
m1 = matrixFactory(intMatrixElems)
33
41
m2 = matrixFactory(intMatrixElems)
42
+
43
+ val pairFactory = BenchmarkPair .ofType(libType)
44
+ pairs = pairElems.map((_1, _2) => pairFactory(_1, _2))
34
45
}
35
46
36
47
@ Benchmark
37
48
def matrixBenchmark = (m1 + m2) * m1 // O(n^3) loops
49
+
50
+ @ Benchmark
51
+ def pairsBenchmark = pairs.foldLeft(0 ){ case (sum, pair) => pair match {
52
+ case BenchmarkPair (i : Int , d : Double ) => 7 * i + 3 * d.toInt + sum
53
+ case BenchmarkPair (c : Char , s : Short ) => 5 * c + 2 * s + sum
54
+ }
55
+ }
38
56
}
0 commit comments