-
-
Save mrbald/d3a0e5eec9bebd53c59327be8e11a6de to your computer and use it in GitHub Desktop.
timeit for scala
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
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