summaryrefslogtreecommitdiff
path: root/test/benchmarks/src/main/scala/scala/collection/immutable/MapBenchmark.scala
blob: a0358d6a1a55bb093b1b11f3cbe18fbc9d5a7dc8 (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
package scala.collection.immutable

import java.util.concurrent.TimeUnit

import org.openjdk.jmh.annotations._
import org.openjdk.jmh.infra._

@BenchmarkMode(Array(Mode.AverageTime))
@Fork(2)
@Threads(1)
@Warmup(iterations = 10)
@Measurement(iterations = 10)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
class MapBenchmark {

  var base: Map[String,String] = _


  @Setup(Level.Trial) def initKeys(): Unit = {
    base = Map("a" -> "a", "b" -> "b", "c" -> "c", "d" -> "d")
  }

  // immutable map is implemented as EmptyMap -> Map1 -> Map2 -> Map3 -> Map4 -> Hashmap
  // add an extra entry to Map4 causes a lot of work, benchmark the transition
  @Benchmark def map4AddElement(bh: Blackhole): Unit = {
    bh.consume(base.updated("e", "e"))
  }
}