Created
July 24, 2012 15:49
-
-
Save Sciss/3170806 to your computer and use it in GitHub Desktop.
Bench3.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
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