Skip to content

Instantly share code, notes, and snippets.

@nrinaudo
Created January 2, 2019 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nrinaudo/fd8032a0346eca3b3617f19a86c74a81 to your computer and use it in GitHub Desktop.
Save nrinaudo/fd8032a0346eca3b3617f19a86c74a81 to your computer and use it in GitHub Desktop.
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")
}
@nrinaudo
Copy link
Author

nrinaudo commented Jan 2, 2019

Scala 2.11.8:

Concatenation: 13.6 ms / 100000 ops
Interpolation: 23.3 ms / 100000 ops

Scala 2.12.8:

Concatenation: 16.2 ms / 100000 ops
Interpolation: 14.9 ms / 100000 ops

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment