Created
January 2, 2019 20:54
-
-
Save nrinaudo/fd8032a0346eca3b3617f19a86c74a81 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
object Test extends App { | |
val benchIterations = 10 | |
val warmupIterations = 10 | |
val repeat = 100000 | |
val a = "foo" | |
val b = 123 | |
def benchOne[U](a : => U): Long = | |
(0 to repeat).foldLeft(0L) { case (total, _) => | |
val time = System.currentTimeMillis() | |
a | |
total + System.currentTimeMillis() - time | |
} | |
def bench[U](a: => U): Double = | |
(0 to benchIterations).map(_ => benchOne(a)).sum / benchIterations.toDouble | |
def warm[U](a: => U): Unit = | |
(0 to warmupIterations).foreach { _ => bench(a)} | |
def run[U](label: String, a: => U): Unit = { | |
warm(a) | |
print(s"$label: ") | |
print(bench(a + b.toString)) | |
println(s" ms / ${repeat} ops") | |
} | |
run("Concatenation", a + b) | |
run("Interpolation", s"$a$b") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Scala 2.11.8:
Scala 2.12.8: