Skip to content

Instantly share code, notes, and snippets.

@amaya382
Last active July 7, 2016 20:09
Show Gist options
  • Save amaya382/a792ebf80dbf9dbd6879 to your computer and use it in GitHub Desktop.
Save amaya382/a792ebf80dbf9dbd6879 to your computer and use it in GitHub Desktop.
Scalaのベンチマーク用スニペット
def timeOf(action: => Unit, noTimes: Int = 10, multiplier: Int = 1): Double = {
import java.util.concurrent.TimeUnit
val runtime = Runtime.getRuntime
val result = (1 to noTimes + 1) map { _ =>
val startTime = System.nanoTime
var i = 0
while (i < multiplier) {
action
i += 1
}
val stopTime = System.nanoTime
runtime.gc()
TimeUnit.MICROSECONDS.convert(stopTime - startTime,
TimeUnit.NANOSECONDS).asInstanceOf[Double]
}
result.sum / noTimes
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment