Last active
April 23, 2020 16:35
-
-
Save rklaehn/26c277b2b5666ec4b372 to your computer and use it in GitHub Desktop.
java vs. scala map microbenchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ichi.bench.Thyme | |
object Bench extends App { | |
final val n = 20 | |
val javaMap = { | |
val r = new java.util.HashMap[Int, Int]() | |
for (i <- 1 to n) | |
r.put(i, i + 1) | |
r | |
} | |
val scalaMap = { | |
var r = Map.empty[Int, Int] | |
for(i <- 1 to n) | |
r = r.updated(i, i + 1) | |
r | |
} | |
val mutableLongMap = { | |
val r = scala.collection.mutable.LongMap.empty[Int] | |
for(i <- 1 to n) | |
r.update(i, i + 1) | |
r | |
} | |
def lookupJava(): Int = { | |
var i = 1 | |
var r = 0 | |
while(i <= n) { | |
r += javaMap.get(i) | |
i += 1 | |
} | |
r | |
} | |
def lookupScalaImmutable(): Int = { | |
var i = 1 | |
var r = 0 | |
while(i <= n) { | |
r += scalaMap(i) | |
i += 1 | |
} | |
r | |
} | |
def lookupScalaMutableLong(): Int = { | |
var i = 1 | |
var r = 0 | |
while(i <= n) { | |
r += mutableLongMap(i) | |
i += 1 | |
} | |
r | |
} | |
val th = ichi.bench.Thyme.warmed(verbose = println, warmth = Thyme.HowWarm.BenchOff) | |
th.pbenchOffWarm("mutableJavaVsImmutableScala")(th.Warm(lookupJava()))(th.Warm(lookupScalaImmutable())) | |
th.pbenchOffWarm("mutableJavaVsMutableLongScala")(th.Warm(lookupJava()))(th.Warm(lookupScalaMutableLong())) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Creating Thyme instances and warming busywork methods... | |
done in 3.75 s | |
Warming up benchmarking... | |
done in 10.19 s | |
Warming up head-to-head benchmarking... | |
done in 4.48 s | |
Benchmark comparison (in 9.242 s): mutableJavaVsImmutableScala | |
Significantly different (p ~= 0) | |
Time ratio: 0.72381 95% CI 0.71746 - 0.73017 (n=30) | |
First 212.9 ns 95% CI 211.8 ns - 214.0 ns | |
Second 154.1 ns 95% CI 153.0 ns - 155.2 ns | |
Benchmark comparison (in 3.566 s): mutableJavaVsMutableLongScala | |
Significantly different (p ~= 0) | |
Time ratio: 0.27282 95% CI 0.27130 - 0.27434 (n=20) | |
First 216.6 ns 95% CI 215.7 ns - 217.4 ns | |
Second 59.08 ns 95% CI 58.86 ns - 59.30 ns | |
Process finished with exit code 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment