Skip to content

Instantly share code, notes, and snippets.

@plokhotnyuk
Last active April 6, 2018 09:55
Show Gist options
  • Save plokhotnyuk/a906b0039406f91b67b1cced25c06361 to your computer and use it in GitHub Desktop.
Save plokhotnyuk/a906b0039406f91b67b1cced25c06361 to your computer and use it in GitHub Desktop.
Option.fold alternatives in a loop over an array of options
/*
[info] Benchmark (distribution) Mode Cnt Score Error Units
[info] ArrayOfOptionBenchmark.fold AllNone thrpt 5 27250400.765 ± 645700.634 ops/s
[info] ArrayOfOptionBenchmark.fold:·gc.alloc.rate AllNone thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.fold:·gc.alloc.rate.norm AllNone thrpt 5 ≈ 10⁻⁵ B/op
[info] ArrayOfOptionBenchmark.fold:·gc.count AllNone thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.fold BothPseudoRandom thrpt 5 7220462.819 ± 491274.950 ops/s
[info] ArrayOfOptionBenchmark.fold:·gc.alloc.rate BothPseudoRandom thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.fold:·gc.alloc.rate.norm BothPseudoRandom thrpt 5 ≈ 10⁻⁴ B/op
[info] ArrayOfOptionBenchmark.fold:·gc.count BothPseudoRandom thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.fold AllSome thrpt 5 7035310.496 ± 291066.050 ops/s
[info] ArrayOfOptionBenchmark.fold:·gc.alloc.rate AllSome thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.fold:·gc.alloc.rate.norm AllSome thrpt 5 ≈ 10⁻⁴ B/op
[info] ArrayOfOptionBenchmark.fold:·gc.count AllSome thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.isDefined AllNone thrpt 5 23620662.613 ± 40441.907 ops/s
[info] ArrayOfOptionBenchmark.isDefined:·gc.alloc.rate AllNone thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.isDefined:·gc.alloc.rate.norm AllNone thrpt 5 ≈ 10⁻⁵ B/op
[info] ArrayOfOptionBenchmark.isDefined:·gc.count AllNone thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.isDefined BothPseudoRandom thrpt 5 15843477.001 ± 60360.711 ops/s
[info] ArrayOfOptionBenchmark.isDefined:·gc.alloc.rate BothPseudoRandom thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.isDefined:·gc.alloc.rate.norm BothPseudoRandom thrpt 5 ≈ 10⁻⁵ B/op
[info] ArrayOfOptionBenchmark.isDefined:·gc.count BothPseudoRandom thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.isDefined AllSome thrpt 5 10541059.654 ± 91603.315 ops/s
[info] ArrayOfOptionBenchmark.isDefined:·gc.alloc.rate AllSome thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.isDefined:·gc.alloc.rate.norm AllSome thrpt 5 ≈ 10⁻⁴ B/op
[info] ArrayOfOptionBenchmark.isDefined:·gc.count AllSome thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.isEmpty AllNone thrpt 5 23537937.521 ± 210037.431 ops/s
[info] ArrayOfOptionBenchmark.isEmpty:·gc.alloc.rate AllNone thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.isEmpty:·gc.alloc.rate.norm AllNone thrpt 5 ≈ 10⁻⁵ B/op
[info] ArrayOfOptionBenchmark.isEmpty:·gc.count AllNone thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.isEmpty BothPseudoRandom thrpt 5 15854973.607 ± 155049.707 ops/s
[info] ArrayOfOptionBenchmark.isEmpty:·gc.alloc.rate BothPseudoRandom thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.isEmpty:·gc.alloc.rate.norm BothPseudoRandom thrpt 5 ≈ 10⁻⁵ B/op
[info] ArrayOfOptionBenchmark.isEmpty:·gc.count BothPseudoRandom thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.isEmpty AllSome thrpt 5 10543673.452 ± 25064.121 ops/s
[info] ArrayOfOptionBenchmark.isEmpty:·gc.alloc.rate AllSome thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.isEmpty:·gc.alloc.rate.norm AllSome thrpt 5 ≈ 10⁻⁴ B/op
[info] ArrayOfOptionBenchmark.isEmpty:·gc.count AllSome thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.isEqNone AllNone thrpt 5 51269174.631 ± 233397.535 ops/s
[info] ArrayOfOptionBenchmark.isEqNone:·gc.alloc.rate AllNone thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.isEqNone:·gc.alloc.rate.norm AllNone thrpt 5 ≈ 10⁻⁵ B/op
[info] ArrayOfOptionBenchmark.isEqNone:·gc.count AllNone thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.isEqNone BothPseudoRandom thrpt 5 12351919.650 ± 279924.804 ops/s
[info] ArrayOfOptionBenchmark.isEqNone:·gc.alloc.rate BothPseudoRandom thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.isEqNone:·gc.alloc.rate.norm BothPseudoRandom thrpt 5 ≈ 10⁻⁴ B/op
[info] ArrayOfOptionBenchmark.isEqNone:·gc.count BothPseudoRandom thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.isEqNone AllSome thrpt 5 10493859.522 ± 95224.358 ops/s
[info] ArrayOfOptionBenchmark.isEqNone:·gc.alloc.rate AllSome thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.isEqNone:·gc.alloc.rate.norm AllSome thrpt 5 ≈ 10⁻⁴ B/op
[info] ArrayOfOptionBenchmark.isEqNone:·gc.count AllSome thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.isInstanceOfNone AllNone thrpt 5 51147435.132 ± 690792.232 ops/s
[info] ArrayOfOptionBenchmark.isInstanceOfNone:·gc.alloc.rate AllNone thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.isInstanceOfNone:·gc.alloc.rate.norm AllNone thrpt 5 ≈ 10⁻⁵ B/op
[info] ArrayOfOptionBenchmark.isInstanceOfNone:·gc.count AllNone thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.isInstanceOfNone BothPseudoRandom thrpt 5 12412033.277 ± 296509.807 ops/s
[info] ArrayOfOptionBenchmark.isInstanceOfNone:·gc.alloc.rate BothPseudoRandom thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.isInstanceOfNone:·gc.alloc.rate.norm BothPseudoRandom thrpt 5 ≈ 10⁻⁴ B/op
[info] ArrayOfOptionBenchmark.isInstanceOfNone:·gc.count BothPseudoRandom thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.isInstanceOfNone AllSome thrpt 5 10506933.922 ± 99160.050 ops/s
[info] ArrayOfOptionBenchmark.isInstanceOfNone:·gc.alloc.rate AllSome thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.isInstanceOfNone:·gc.alloc.rate.norm AllSome thrpt 5 ≈ 10⁻⁴ B/op
[info] ArrayOfOptionBenchmark.isInstanceOfNone:·gc.count AllSome thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.isInstanceOfSome AllNone thrpt 5 23409240.114 ± 70086.268 ops/s
[info] ArrayOfOptionBenchmark.isInstanceOfSome:·gc.alloc.rate AllNone thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.isInstanceOfSome:·gc.alloc.rate.norm AllNone thrpt 5 ≈ 10⁻⁵ B/op
[info] ArrayOfOptionBenchmark.isInstanceOfSome:·gc.count AllNone thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.isInstanceOfSome BothPseudoRandom thrpt 5 11383554.138 ± 63801.084 ops/s
[info] ArrayOfOptionBenchmark.isInstanceOfSome:·gc.alloc.rate BothPseudoRandom thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.isInstanceOfSome:·gc.alloc.rate.norm BothPseudoRandom thrpt 5 ≈ 10⁻⁴ B/op
[info] ArrayOfOptionBenchmark.isInstanceOfSome:·gc.count BothPseudoRandom thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.isInstanceOfSome AllSome thrpt 5 10708363.806 ± 26680.000 ops/s
[info] ArrayOfOptionBenchmark.isInstanceOfSome:·gc.alloc.rate AllSome thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.isInstanceOfSome:·gc.alloc.rate.norm AllSome thrpt 5 ≈ 10⁻⁴ B/op
[info] ArrayOfOptionBenchmark.isInstanceOfSome:·gc.count AllSome thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.mapGetOrElse AllNone thrpt 5 10389980.685 ± 63668.150 ops/s
[info] ArrayOfOptionBenchmark.mapGetOrElse:·gc.alloc.rate AllNone thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.mapGetOrElse:·gc.alloc.rate.norm AllNone thrpt 5 ≈ 10⁻⁴ B/op
[info] ArrayOfOptionBenchmark.mapGetOrElse:·gc.count AllNone thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.mapGetOrElse BothPseudoRandom thrpt 5 3481016.526 ± 65999.877 ops/s
[info] ArrayOfOptionBenchmark.mapGetOrElse:·gc.alloc.rate BothPseudoRandom thrpt 5 1451.146 ± 27.634 MB/sec
[info] ArrayOfOptionBenchmark.mapGetOrElse:·gc.alloc.rate.norm BothPseudoRandom thrpt 5 656.000 ± 0.001 B/op
[info] ArrayOfOptionBenchmark.mapGetOrElse:·gc.churn.PS_Eden_Space BothPseudoRandom thrpt 5 1480.440 ± 702.794 MB/sec
[info] ArrayOfOptionBenchmark.mapGetOrElse:·gc.churn.PS_Eden_Space.norm BothPseudoRandom thrpt 5 669.148 ± 313.417 B/op
[info] ArrayOfOptionBenchmark.mapGetOrElse:·gc.churn.PS_Survivor_Space BothPseudoRandom thrpt 5 0.079 ± 0.132 MB/sec
[info] ArrayOfOptionBenchmark.mapGetOrElse:·gc.churn.PS_Survivor_Space.norm BothPseudoRandom thrpt 5 0.036 ± 0.059 B/op
[info] ArrayOfOptionBenchmark.mapGetOrElse:·gc.count BothPseudoRandom thrpt 5 22.000 counts
[info] ArrayOfOptionBenchmark.mapGetOrElse:·gc.time BothPseudoRandom thrpt 5 16.000 ms
[info] ArrayOfOptionBenchmark.mapGetOrElse AllSome thrpt 5 7097320.450 ± 30143.164 ops/s
[info] ArrayOfOptionBenchmark.mapGetOrElse:·gc.alloc.rate AllSome thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.mapGetOrElse:·gc.alloc.rate.norm AllSome thrpt 5 ≈ 10⁻⁴ B/op
[info] ArrayOfOptionBenchmark.mapGetOrElse:·gc.count AllSome thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.patmatNoneFirst AllNone thrpt 5 28409621.860 ± 715218.351 ops/s
[info] ArrayOfOptionBenchmark.patmatNoneFirst:·gc.alloc.rate AllNone thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.patmatNoneFirst:·gc.alloc.rate.norm AllNone thrpt 5 ≈ 10⁻⁵ B/op
[info] ArrayOfOptionBenchmark.patmatNoneFirst:·gc.count AllNone thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.patmatNoneFirst BothPseudoRandom thrpt 5 13058497.131 ± 780357.033 ops/s
[info] ArrayOfOptionBenchmark.patmatNoneFirst:·gc.alloc.rate BothPseudoRandom thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.patmatNoneFirst:·gc.alloc.rate.norm BothPseudoRandom thrpt 5 ≈ 10⁻⁴ B/op
[info] ArrayOfOptionBenchmark.patmatNoneFirst:·gc.count BothPseudoRandom thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.patmatNoneFirst AllSome thrpt 5 10467297.007 ± 19623.725 ops/s
[info] ArrayOfOptionBenchmark.patmatNoneFirst:·gc.alloc.rate AllSome thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.patmatNoneFirst:·gc.alloc.rate.norm AllSome thrpt 5 ≈ 10⁻⁴ B/op
[info] ArrayOfOptionBenchmark.patmatNoneFirst:·gc.count AllSome thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.patmatSomeFirst AllNone thrpt 5 12196853.036 ± 52148.273 ops/s
[info] ArrayOfOptionBenchmark.patmatSomeFirst:·gc.alloc.rate AllNone thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.patmatSomeFirst:·gc.alloc.rate.norm AllNone thrpt 5 ≈ 10⁻⁴ B/op
[info] ArrayOfOptionBenchmark.patmatSomeFirst:·gc.count AllNone thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.patmatSomeFirst BothPseudoRandom thrpt 5 9519488.536 ± 829610.135 ops/s
[info] ArrayOfOptionBenchmark.patmatSomeFirst:·gc.alloc.rate BothPseudoRandom thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.patmatSomeFirst:·gc.alloc.rate.norm BothPseudoRandom thrpt 5 ≈ 10⁻⁴ B/op
[info] ArrayOfOptionBenchmark.patmatSomeFirst:·gc.count BothPseudoRandom thrpt 5 ≈ 0 counts
[info] ArrayOfOptionBenchmark.patmatSomeFirst AllSome thrpt 5 10685989.040 ± 57893.233 ops/s
[info] ArrayOfOptionBenchmark.patmatSomeFirst:·gc.alloc.rate AllSome thrpt 5 ≈ 10⁻⁴ MB/sec
[info] ArrayOfOptionBenchmark.patmatSomeFirst:·gc.alloc.rate.norm AllSome thrpt 5 ≈ 10⁻⁴ B/op
[info] ArrayOfOptionBenchmark.patmatSomeFirst:·gc.count AllSome thrpt 5 ≈ 0 counts
*/
import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
@State(Scope.Thread)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
@Fork(value = 1, jvmArgs = Array(
"-server",
"-Xms1g",
"-Xmx1g",
"-XX:NewSize=512m",
"-XX:MaxNewSize=512m",
"-XX:InitialCodeCacheSize=256m",
"-XX:ReservedCodeCacheSize=256m",
"-XX:-UseBiasedLocking",
"-XX:+AlwaysPreTouch"
))
@BenchmarkMode(Array(Mode.Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
class ArrayOfOptionBenchmark {
@Param(Array("AllNone", "BothPseudoRandom", "AllSome"))
var distribution: String = _
var opts: Array[Option[String]] = _
@Setup
def setup(): Unit = opts = {
var h = -2128831035
Array.fill(100)(distribution match {
case "AllNone" => None
case "AllSome" => Some("str")
case _ =>
h = (h + 13) * 16777619
if ((h & 1024) == 0) None else Some("str")
})
}
@Benchmark
def fold: Int = {
val os = opts
val l = os.length
var s = 0
var i = 0
while (i < l) {
s += os(i).fold(0)(_.length)
i += 1
}
s
}
@Benchmark
def mapGetOrElse: Int = {
val os = opts
val l = os.length
var s = 0
var i = 0
while (i < l) {
s += os(i).map(_.length).getOrElse(0)
i += 1
}
s
}
@Benchmark
def patmatNoneFirst: Int = {
val os = opts
val l = os.length
var s = 0
var i = 0
while (i < l) {
s += {
os(i) match {
case None => 0
case Some(str) => str.length
}
}
i += 1
}
s
}
@Benchmark
def patmatSomeFirst: Int = {
val os = opts
val l = os.length
var s = 0
var i = 0
while (i < l) {
s += {
os(i) match {
case Some(str) => str.length
case None => 0
}
}
i += 1
}
s
}
@Benchmark
def isEmpty: Int = {
val os = opts
val l = os.length
var s = 0
var i = 0
while (i < l) {
s += {
val opt = os(i)
if (opt.isEmpty) 0 else opt.get.length
}
i += 1
}
s
}
@Benchmark
def isDefined: Int = {
val os = opts
val l = os.length
var s = 0
var i = 0
while (i < l) {
s += {
val opt = os(i)
if (opt.isDefined) opt.get.length else 0
}
i += 1
}
s
}
@Benchmark
def isInstanceOfSome: Int = {
val os = opts
val l = os.length
var s = 0
var i = 0
while (i < l) {
s += {
val opt = os(i)
if (opt.isInstanceOf[Some[_]]) opt.get.length else 0
}
i += 1
}
s
}
@Benchmark
def isInstanceOfNone: Int = {
val os = opts
val l = os.length
var s = 0
var i = 0
while (i < l) {
s += {
val opt = os(i)
if (opt.isInstanceOf[None.type]) 0 else opt.get.length
}
i += 1
}
s
}
@Benchmark
def isEqNone: Int = {
val os = opts
val l = os.length
var s = 0
var i = 0
while (i < l) {
s += {
val opt = os(i)
if (opt eq None) 0 else opt.get.length
}
i += 1
}
s
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment