Last active
July 7, 2016 20:09
-
-
Save amaya382/a792ebf80dbf9dbd6879 to your computer and use it in GitHub Desktop.
Scalaのベンチマーク用スニペット
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
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