Skip to content

Instantly share code, notes, and snippets.

@Sciss
Created July 24, 2012 15:49
Show Gist options
  • Save Sciss/3170806 to your computer and use it in GitHub Desktop.
Save Sciss/3170806 to your computer and use it in GitHub Desktop.
Bench3.scala
package benchmark
object Bench3 extends App {
val num: Int = 1000
def time[A](f: => A) : (A, Long) = {
val start = System.nanoTime()
val res = f
res -> (System.nanoTime() - start)
}
def test(times: Int) : (Int, Long) = time {
var i,j,k = 0
var x: Any = null
var res = 0
while(i < times) {
while(j < num) {
while(k < num) {
x = new Object()
res += x.hashCode // need to use `x` to avoid interference from HotSpot smartness
k += 1
}
k = 0
j += 1
}
j = 0
i += 1
}
res
}
// warmup
test(200)
// measured run
val (_, elapsed) = test(2000)
println( "Result = " + elapsed/2000.0 + " ns" )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment