Created
July 3, 2010 15:57
-
-
Save huynhjl/462653 to your computer and use it in GitHub Desktop.
lots and time
This file contains 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
// Borrowed from | |
// http://stackoverflow.com/questions/3102872/what-is-the-fastest-way-to-sum-a-collection-in-scala/3103800#3103800 | |
def time[F](f: => F) = { | |
val t0 = System.nanoTime | |
val ans = f | |
printf("Elapsed: %.3f\n",1e-9*(System.nanoTime-t0)) | |
ans | |
} | |
def lots[F](n: Int, f: => F): F = if (n <= 1) f else { f; lots(n-1,f) } | |
def bench[F](what:String, n:Int, f: => F) = { | |
println(what + ": warming...") | |
lots(n, f) | |
time(what, f) | |
} | |
def bench[F,G](what:String, n:Int, fwarm: => F, f: => G) = { | |
println(what + ": warming...") | |
lots(n, fwarm) | |
time(what, f) | |
} | |
// use like this: | |
// lots(3, time( lots(100, somefunction ) ) | |
// also: http://wikis.sun.com/display/HotSpotInternals/MicroBenchmarks | |
// java -XX:+PrintCompilation -cp "classes;/path/to/lib/scala-library.jar" ClassName |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment