Skip to content

Instantly share code, notes, and snippets.

@julienrf
Last active February 13, 2018 20:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julienrf/f1cb2b062cd9783a35e2f35778959c76 to your computer and use it in GitHub Desktop.
Save julienrf/f1cb2b062cd9783a35e2f35778959c76 to your computer and use it in GitHub Desktop.
package benchmarks.vector
import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
import org.openjdk.jmh.infra.Blackhole
import strawman.collection.immutable
@BenchmarkMode(scala.Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
@Warmup(iterations = 8)
@Measurement(iterations = 8)
@State(Scope.Benchmark)
class Old {
@Param(scala.Array("1", "3", "8", "17", "282", "4096", "131070", "7312102"))
var size: Int = _
var xs: scala.collection.immutable.Vector[Long] = _
def fresh(n: Int) = scala.collection.immutable.Vector((1 to n).map(_.toLong): _*)
@Setup(Level.Trial)
def initTrial(): Unit = {
xs = fresh(size)
}
@Benchmark
def filter(bh: Blackhole) = bh.consume(xs.filter(x => x % 2 == 0))
@Benchmark
def map(bh: Blackhole) = bh.consume(xs.map(x => x + 1))
@Benchmark
def flatMap(bh: Blackhole) = bh.consume(xs.flatMap(x => (scala.collection.immutable.Vector.newBuilder[Long] += x).result))
}
@BenchmarkMode(scala.Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
@Warmup(iterations = 8)
@Measurement(iterations = 8)
@State(Scope.Benchmark)
class ViewBased {
@Param(scala.Array("1", "3", "8", "17", "282", "4096", "131070", "7312102"))
var size: Int = _
var xs: immutable.Vector2[Long] = _
def fresh(n: Int) = immutable.Vector2((1 to n).map(_.toLong): _*)
@Setup(Level.Trial)
def initTrial(): Unit = {
xs = fresh(size)
}
@Benchmark
def filter(bh: Blackhole) = bh.consume(xs.filter(x => x % 2 == 0))
@Benchmark
def map(bh: Blackhole) = bh.consume(xs.map(x => x + 1))
@Benchmark
def flatMap(bh: Blackhole) = bh.consume(xs.flatMap(x => (immutable.Vector2.newBuilder[Long]() += x).result()))
}
@BenchmarkMode(scala.Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
@Warmup(iterations = 8)
@Measurement(iterations = 8)
@State(Scope.Benchmark)
class BuilderBased {
@Param(scala.Array("1", "3", "8", "17", "282", "4096", "131070", "7312102"))
var size: Int = _
var xs: immutable.Vector[Long] = _
def fresh(n: Int) = immutable.Vector((1 to n).map(_.toLong): _*)
@Setup(Level.Trial)
def initTrial(): Unit = {
xs = fresh(size)
}
@Benchmark
def filter(bh: Blackhole) = bh.consume(xs.filter(x => x % 2 == 0))
@Benchmark
def map(bh: Blackhole) = bh.consume(xs.map(x => x + 1))
@Benchmark
def flatMap(bh: Blackhole) = bh.consume(xs.flatMap(x => (immutable.Vector.newBuilder[Long]() += x).result()))
}
[info] Benchmark (size) Mode Cnt Score Error Units
[info] BuilderBased.filter 1 avgt 8 44.930 ± 4.164 ns/op
[info] BuilderBased.filter 3 avgt 8 52.323 ± 9.747 ns/op
[info] BuilderBased.filter 8 avgt 8 68.442 ± 1.895 ns/op
[info] BuilderBased.filter 17 avgt 8 108.401 ± 5.541 ns/op
[info] BuilderBased.filter 282 avgt 8 1565.754 ± 14.119 ns/op
[info] BuilderBased.filter 4096 avgt 8 24785.466 ± 1011.678 ns/op
[info] BuilderBased.filter 131070 avgt 8 1047994.122 ± 308599.140 ns/op
[info] BuilderBased.filter 7312102 avgt 8 52074486.939 ± 1847648.884 ns/op
[info] BuilderBased.flatMap 1 avgt 8 80.891 ± 0.792 ns/op
[info] BuilderBased.flatMap 3 avgt 8 176.807 ± 13.307 ns/op
[info] BuilderBased.flatMap 8 avgt 8 462.309 ± 102.470 ns/op
[info] BuilderBased.flatMap 17 avgt 8 1016.279 ± 347.843 ns/op
[info] BuilderBased.flatMap 282 avgt 8 17531.227 ± 4244.889 ns/op
[info] BuilderBased.flatMap 4096 avgt 8 207155.545 ± 23871.312 ns/op
[info] BuilderBased.flatMap 131070 avgt 8 6368365.396 ± 139554.397 ns/op
[info] BuilderBased.flatMap 7312102 avgt 8 504888745.333 ± 58615016.112 ns/op
[info] BuilderBased.map 1 avgt 8 38.851 ± 0.652 ns/op
[info] BuilderBased.map 3 avgt 8 55.219 ± 9.765 ns/op
[info] BuilderBased.map 8 avgt 8 65.479 ± 2.962 ns/op
[info] BuilderBased.map 17 avgt 8 122.438 ± 17.251 ns/op
[info] BuilderBased.map 282 avgt 8 1779.377 ± 568.804 ns/op
[info] BuilderBased.map 4096 avgt 8 33908.179 ± 11146.948 ns/op
[info] BuilderBased.map 131070 avgt 8 1086201.142 ± 114751.491 ns/op
[info] BuilderBased.map 7312102 avgt 8 71805433.002 ± 8070553.492 ns/op
[info] Old.filter 1 avgt 8 37.949 ± 4.292 ns/op
[info] Old.filter 3 avgt 8 70.985 ± 1.529 ns/op
[info] Old.filter 8 avgt 8 92.744 ± 1.062 ns/op
[info] Old.filter 17 avgt 8 119.924 ± 2.520 ns/op
[info] Old.filter 282 avgt 8 2421.541 ± 41.832 ns/op
[info] Old.filter 4096 avgt 8 33748.513 ± 1092.256 ns/op
[info] Old.filter 131070 avgt 8 1417234.248 ± 819354.779 ns/op
[info] Old.filter 7312102 avgt 8 62460809.166 ± 12616117.355 ns/op
[info] Old.flatMap 1 avgt 8 193.063 ± 43.570 ns/op
[info] Old.flatMap 3 avgt 8 431.032 ± 123.561 ns/op
[info] Old.flatMap 8 avgt 8 870.994 ± 58.165 ns/op
[info] Old.flatMap 17 avgt 8 1706.075 ± 93.230 ns/op
[info] Old.flatMap 282 avgt 8 30562.716 ± 531.488 ns/op
[info] Old.flatMap 4096 avgt 8 376864.629 ± 9761.531 ns/op
[info] Old.flatMap 131070 avgt 8 13775979.917 ± 1014919.040 ns/op
[info] Old.flatMap 7312102 avgt 8 1095901950.063 ± 364554267.694 ns/op
[info] Old.map 1 avgt 8 53.605 ± 13.671 ns/op
[info] Old.map 3 avgt 8 73.158 ± 9.977 ns/op
[info] Old.map 8 avgt 8 118.213 ± 5.378 ns/op
[info] Old.map 17 avgt 8 221.140 ± 23.888 ns/op
[info] Old.map 282 avgt 8 3604.628 ± 634.721 ns/op
[info] Old.map 4096 avgt 8 54720.673 ± 15096.804 ns/op
[info] Old.map 131070 avgt 8 1480161.773 ± 479342.901 ns/op
[info] Old.map 7312102 avgt 8 117995635.052 ± 22365315.065 ns/op
[info] ViewBased.filter 1 avgt 8 60.663 ± 24.151 ns/op
[info] ViewBased.filter 3 avgt 8 84.774 ± 30.378 ns/op
[info] ViewBased.filter 8 avgt 8 88.824 ± 6.965 ns/op
[info] ViewBased.filter 17 avgt 8 146.730 ± 17.577 ns/op
[info] ViewBased.filter 282 avgt 8 1801.047 ± 225.009 ns/op
[info] ViewBased.filter 4096 avgt 8 23869.174 ± 1296.844 ns/op
[info] ViewBased.filter 131070 avgt 8 1030685.863 ± 59313.906 ns/op
[info] ViewBased.filter 7312102 avgt 8 79635715.971 ± 1731782.729 ns/op
[info] ViewBased.flatMap 1 avgt 8 108.667 ± 19.126 ns/op
[info] ViewBased.flatMap 3 avgt 8 192.086 ± 5.382 ns/op
[info] ViewBased.flatMap 8 avgt 8 420.289 ± 5.819 ns/op
[info] ViewBased.flatMap 17 avgt 8 855.749 ± 117.734 ns/op
[info] ViewBased.flatMap 282 avgt 8 14516.967 ± 2640.764 ns/op
[info] ViewBased.flatMap 4096 avgt 8 207255.077 ± 4487.462 ns/op
[info] ViewBased.flatMap 131070 avgt 8 8133272.365 ± 353868.206 ns/op
[info] ViewBased.flatMap 7312102 avgt 8 951327870.125 ± 731984176.092 ns/op
[info] ViewBased.map 1 avgt 8 54.911 ± 6.998 ns/op
[info] ViewBased.map 3 avgt 8 61.562 ± 1.094 ns/op
[info] ViewBased.map 8 avgt 8 74.425 ± 1.285 ns/op
[info] ViewBased.map 17 avgt 8 111.111 ± 1.749 ns/op
[info] ViewBased.map 282 avgt 8 2272.941 ± 263.600 ns/op
[info] ViewBased.map 4096 avgt 8 62293.278 ± 13050.090 ns/op
[info] ViewBased.map 131070 avgt 8 1697725.384 ± 340911.846 ns/op
[info] ViewBased.map 7312102 avgt 8 91044984.556 ± 17932214.736 ns/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment