Skip to content

Instantly share code, notes, and snippets.

@bdarfler
Created September 8, 2011 15:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdarfler/1203677 to your computer and use it in GitHub Desktop.
Save bdarfler/1203677 to your computer and use it in GitHub Desktop.
Scala Microbenchmark Runner
def benchmark(runs: Long, reps: Long, funct: => Any) = {
// warmup
println("Warming Up")
var i = 0
while ( i < 25000 ) { funct; i = i + 1 }
// test runs
println("Running Test")
var j = 0
while ( j < runs ) {
var k = 0
val start = System.nanoTime()
while ( k < reps ) { funct; k = k + 1 }
val avgNanos = (System.nanoTime() - start) / reps
val avgMillis = avgNanos / 1000000f
println("Avg Execution Time: %s ms".format(avgMillis))
j = j + 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment