Skip to content

Instantly share code, notes, and snippets.

@mrbald
Forked from atmb4u/timeit.scala
Last active October 2, 2018 13:14
Show Gist options
  • Save mrbald/d3a0e5eec9bebd53c59327be8e11a6de to your computer and use it in GitHub Desktop.
Save mrbald/d3a0e5eec9bebd53c59327be8e11a6de to your computer and use it in GitHub Desktop.
timeit for scala
def timeit (warmups: Int, trials: Int) (block: => Unit): Unit = {
import Math.{min,max}
for (i <- 1 to warmups) block
var total = 0.0
var best = Long.MaxValue
var worst = Long.MinValue
for (i <- 1 to trials) {
val beg = System.nanoTime()
block
val end = System.nanoTime()
val elapsed = max(0, end - beg)
total += elapsed
best = min(best, elapsed)
worst = max(worst, elapsed)
}
println(s"$warmups warmups / $trials trials best|mean|worst = $best|${total/trials.toDouble}|$worst ns")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment