summaryrefslogtreecommitdiff
path: root/test/benchmarks/src/main/scala/scala/collection/mutable/HashMapBenchmark.scala
blob: 3f01d154e9341b2dbd4e2cc128661c9ace86a677 (plain) (blame)
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
package scala.collection.mutable

import org.openjdk.jmh.annotations._
import org.openjdk.jmh.infra._
import org.openjdk.jmh.runner.IterationType
import benchmark._
import java.util.concurrent.TimeUnit

import scala.collection.mutable

@BenchmarkMode(Array(Mode.AverageTime))
@Fork(2)
@Threads(1)
@Warmup(iterations = 10)
@Measurement(iterations = 10)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
class HashMapBenchmark {
  @Param(Array("10", "100", "1000"))
  var size: Int = _

  var existingKeys: Array[Any] = _
  var missingKeys: Array[Any] = _

  @Setup(Level.Trial) def initKeys(): Unit = {
    existingKeys = (0 to size).map(i => (i % 4) match {
      case 0 => i.toString
      case 1 => i.toChar
      case 2 => i.toDouble
      case 3 => i.toInt
    }).toArray
    missingKeys = (size to 2 * size).toArray
  }

  var map = new mutable.HashMap[Any, Any]

  @Setup(Level.Invocation) def initializeMutable = existingKeys.foreach(v => map.put(v, v))

  @TearDown(Level.Invocation) def tearDown = map.clear()

  @Benchmark def getOrElseUpdate(bh: Blackhole): Unit = {
    var i = 0;
    while (i < size) {
      bh.consume(map.getOrElseUpdate(existingKeys(i), -1))
      bh.consume(map.getOrElseUpdate(missingKeys(i), -1))
      i += 1
    }
  }

  @Benchmark def get(bh: Blackhole): Unit = {
    var i = 0;
    while (i < size) {
      bh.consume(map.get(existingKeys(i), -1))
      bh.consume(map.get(missingKeys(i), -1))
      i += 1
    }
  }

  @Benchmark def put(bh: Blackhole): Any = {
    var map = new mutable.HashMap[Any, Any]

    var i = 0;
    while (i < size) {
      map.put(existingKeys(i), i)
      i += 1
    }

    map
  }
}