From aefc8f4936e35b4f835fa2ec70d56443bbd7fa06 Mon Sep 17 00:00:00 2001 From: Rory Graves Date: Sat, 4 Mar 2017 07:52:04 +0100 Subject: Add benchmarks for Map4 to HashMap and Set4 to HashSet transitions --- .../scala/collection/immutable/MapBenchmark.scala | 29 ++++++++++++++++++++++ .../scala/collection/immutable/SetBenchmark.scala | 29 ++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 test/benchmarks/src/main/scala/scala/collection/immutable/MapBenchmark.scala create mode 100644 test/benchmarks/src/main/scala/scala/collection/immutable/SetBenchmark.scala (limited to 'test') diff --git a/test/benchmarks/src/main/scala/scala/collection/immutable/MapBenchmark.scala b/test/benchmarks/src/main/scala/scala/collection/immutable/MapBenchmark.scala new file mode 100644 index 0000000000..a0358d6a1a --- /dev/null +++ b/test/benchmarks/src/main/scala/scala/collection/immutable/MapBenchmark.scala @@ -0,0 +1,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")) + } +} diff --git a/test/benchmarks/src/main/scala/scala/collection/immutable/SetBenchmark.scala b/test/benchmarks/src/main/scala/scala/collection/immutable/SetBenchmark.scala new file mode 100644 index 0000000000..9330626691 --- /dev/null +++ b/test/benchmarks/src/main/scala/scala/collection/immutable/SetBenchmark.scala @@ -0,0 +1,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 SetBenchmark { + + var base: Set[String] = _ + + + @Setup(Level.Trial) def initKeys(): Unit = { + base = Set("a", "b", "c", "d") + } + + // immutable map is implemented as EmptySet -> Set1 -> Set2 -> Set3 -> Set4 -> HashSet + // add an extra entry to Set4 causes a lot of work, benchmark the transition + @Benchmark def set4AddElement(bh: Blackhole): Unit = { + bh.consume(base + "e") + } +} -- cgit v1.2.3