From eb5c51383a63c5c3420e53ef021607ff5fd20296 Mon Sep 17 00:00:00 2001 From: Rory Graves Date: Mon, 17 Oct 2016 20:25:31 +0100 Subject: Optimised implementation of List.filter/filterNot --- .../scala/collection/immutable/ListBenchmark.scala | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 test/benchmarks/src/main/scala/scala/collection/immutable/ListBenchmark.scala (limited to 'test/benchmarks/src') 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..5ed30de7f6 --- /dev/null +++ b/test/benchmarks/src/main/scala/scala/collection/immutable/ListBenchmark.scala @@ -0,0 +1,57 @@ +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) + } +} -- cgit v1.2.3 From 7e89663931075e8652f3008881ec55d5803557cc Mon Sep 17 00:00:00 2001 From: Rory Graves Date: Sat, 28 Jan 2017 21:50:50 +0000 Subject: Add benchmark for List.mapConserve --- .../scala/scala/collection/immutable/ListBenchmark.scala | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'test/benchmarks/src') 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 5ed30de7f6..94844dcae2 100644 --- a/test/benchmarks/src/main/scala/scala/collection/immutable/ListBenchmark.scala +++ b/test/benchmarks/src/main/scala/scala/collection/immutable/ListBenchmark.scala @@ -54,4 +54,19 @@ class ListBenchmark { @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) + } } -- cgit v1.2.3