Created
May 8, 2014 08:22
-
-
Save ericpony/52c4d39f6a00ceff8f5a to your computer and use it in GitHub Desktop.
Simple scala profiling
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
/*************** Method I ****************/ | |
def time[R](block: => R): R = { | |
val t0 = System.nanoTime() | |
val result = block // call-by-name | |
val t1 = System.nanoTime() | |
println("Elapsed time: " + (t1 - t0)/1000000 + "ms") | |
result | |
} | |
// Now wrap your method calls with profiler | |
val result = time { 1 to 1000 sum } | |
/*************** Method II ****************/ | |
import System.currentTimeMillis | |
def profile[R](code: => R, t: Long = currentTimeMillis) = (code, currentTimeMillis - t) | |
// usage: | |
val (result, time) = profile { /* block of code to be profiled*/ } | |
val (result2, time2) = profile methodToBeProfiled(foo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment