diff options
Diffstat (limited to 'test/benchmarks/src/main/scala/scala/collection/immutable')
5 files changed, 150 insertions, 6 deletions
diff --git a/test/benchmarks/src/main/scala/scala/collection/immutable/HashMapBenchmark.scala b/test/benchmarks/src/main/scala/scala/collection/immutable/HashMapBenchmark.scala new file mode 100644 index 0000000000..134cd6879b --- /dev/null +++ b/test/benchmarks/src/main/scala/scala/collection/immutable/HashMapBenchmark.scala @@ -0,0 +1,56 @@ +package scala.collection.immutable + +import org.openjdk.jmh.annotations._ +import org.openjdk.jmh.infra._ +import org.openjdk.jmh.runner.IterationType +import benchmark._ +import java.util.concurrent.TimeUnit + +@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: collection.immutable.HashMap[Any, Any] = null + + @Setup(Level.Trial) def initialize = { + map = collection.immutable.HashMap(existingKeys.map(x => (x, x)) : _*) + } + + @Benchmark def contains(bh: Blackhole): Unit = { + var i = 0; + while (i < size) { + bh.consume(map.contains(existingKeys(i))) + bh.consume(map.contains(missingKeys(i))) + i += 1 + } + } + + @Benchmark def get(bh: Blackhole): Unit = { + var i = 0; + while (i < size) { + bh.consume(map.get(existingKeys(i))) + bh.consume(map.get(missingKeys(i))) + i += 1 + } + } +} diff --git a/test/benchmarks/src/main/scala/scala/collection/immutable/ListBenchmark.scala b/test/benchmarks/src/main/scala/scala/collection/immutable/ListBenchmark.scala index 94844dcae2..36e2518993 100644 --- a/test/benchmarks/src/main/scala/scala/collection/immutable/ListBenchmark.scala +++ b/test/benchmarks/src/main/scala/scala/collection/immutable/ListBenchmark.scala @@ -23,12 +23,14 @@ class ListBenchmark { var values: List[Content] = _ var mid: Content = _ var last: Content = _ + var replacement: Content = _ @Setup(Level.Trial) def initKeys(): Unit = { values = List.tabulate(size)(v => Content(v)) mid = Content(size / 2) last = Content(Math.max(0,size -1)) + replacement = Content(size * 2 + 1) } @Benchmark def filter_includeAll: Any = { @@ -55,18 +57,14 @@ class ListBenchmark { values.filter(v => v.value == last.value) } - @Setup(Level.Trial) def initKeys(): Unit = { - values = List.tabulate(size)(n => if (n == size / 2) "mid" else "") - } - @Benchmark def mapConserve_identity: Any = { values.mapConserve(x => x) } @Benchmark def mapConserve_modifyAll: Any = { - values.mapConserve(x => "replace") + values.mapConserve(x => replacement) } @Benchmark def mapConserve_modifyMid: Any = { - values.mapConserve(x => if (x == "mid") "replace" else x) + values.mapConserve(x => if (x == mid) replacement else x) } } 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") + } +} diff --git a/test/benchmarks/src/main/scala/scala/collection/immutable/VectorMapBenchmark.scala b/test/benchmarks/src/main/scala/scala/collection/immutable/VectorMapBenchmark.scala new file mode 100644 index 0000000000..61e621dcdf --- /dev/null +++ b/test/benchmarks/src/main/scala/scala/collection/immutable/VectorMapBenchmark.scala @@ -0,0 +1,32 @@ +package scala.collection.immutable + +import org.openjdk.jmh.annotations._ +import org.openjdk.jmh.infra._ +import org.openjdk.jmh.runner.IterationType +import benchmark._ +import java.util.concurrent.TimeUnit + +@BenchmarkMode(Array(Mode.AverageTime)) +@Fork(2) +@Threads(1) +@Warmup(iterations = 10) +@Measurement(iterations = 10) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@State(Scope.Benchmark) +class VectorMapBenchmark { + @Param(Array("10", "100", "1000")) + var size: Int = _ + + var values: Vector[Any] = _ + + @Setup(Level.Trial) def initKeys(): Unit = { + values = (0 to size).map(i => (i % 4) match { + case 0 => i.toString + case 1 => i.toChar + case 2 => i.toDouble + case 3 => i.toInt + }).toVector + } + + @Benchmark def groupBy = values.groupBy(_.getClass) +} |