summaryrefslogtreecommitdiff
path: root/test/benchmarks/src
diff options
context:
space:
mode:
Diffstat (limited to 'test/benchmarks/src')
-rw-r--r--test/benchmarks/src/main/scala/scala/collection/immutable/HashMapBenchmark.scala56
-rw-r--r--test/benchmarks/src/main/scala/scala/collection/immutable/ListBenchmark.scala72
2 files changed, 128 insertions, 0 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
new file mode 100644
index 0000000000..94844dcae2
--- /dev/null
+++ b/test/benchmarks/src/main/scala/scala/collection/immutable/ListBenchmark.scala
@@ -0,0 +1,72 @@
+package scala.collection.immutable
+
+import java.util.concurrent.TimeUnit
+
+import org.openjdk.jmh.annotations._
+
+object ListBenchmark {
+ case class Content(value: Int)
+}
+
+@BenchmarkMode(Array(Mode.AverageTime))
+@Fork(2)
+@Threads(1)
+@Warmup(iterations = 10)
+@Measurement(iterations = 10)
+@OutputTimeUnit(TimeUnit.NANOSECONDS)
+@State(Scope.Benchmark)
+class ListBenchmark {
+ import ListBenchmark._
+ @Param(Array("0", "1", "10", "100", "1000"))
+ var size: Int = _
+
+ var values: List[Content] = _
+ var mid: Content = _
+ var last: 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))
+ }
+
+ @Benchmark def filter_includeAll: Any = {
+ values.filter(v => true)
+ }
+
+ @Benchmark def filter_excludeAll: Any = {
+ values.filter(_ => false)
+ }
+
+ @Benchmark def filter_exc_mid: Any = {
+ values.filter(v => v.value != mid.value)
+ }
+
+ @Benchmark def filter_from_mid: Any = {
+ values.filter(v => v.value <= mid.value)
+ }
+
+ @Benchmark def filter_exc_last: Any = {
+ values.filter(v => v.value != last.value)
+ }
+
+ @Benchmark def filter_only_last: Any = {
+ 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")
+ }
+ @Benchmark def mapConserve_modifyMid: Any = {
+ values.mapConserve(x => if (x == "mid") "replace" else x)
+ }
+}