Skip to content

Instantly share code, notes, and snippets.

@ericpony
Created May 8, 2014 08:22
Show Gist options
  • Save ericpony/52c4d39f6a00ceff8f5a to your computer and use it in GitHub Desktop.
Save ericpony/52c4d39f6a00ceff8f5a to your computer and use it in GitHub Desktop.
Simple scala profiling
/*************** 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