Skip to content

Instantly share code, notes, and snippets.

@d6y
Created July 22, 2012 15:55
Show Gist options
  • Select an option

  • Save d6y/3160096 to your computer and use it in GitHub Desktop.

Select an option

Save d6y/3160096 to your computer and use it in GitHub Desktop.
def time[R](opname: String)(block: => R) = {
val start_time = System.nanoTime()
var success = true
try {
block
} catch {
case ex: Throwable => {
success = false;
throw ex
}
} finally {
val diff = System.nanoTime() - start_time
println(opname + " " + diff + " "+success)
}
}
def f():Integer = {
val answer = time("a") {
val r = new Random()
if (r.nextInt(100) > 50)
throw new java.io.IOException("Boom")
else
"42"
}
println ("the answer, "+answer+" is of type "+answer.getClass())
val seven:Integer = time("b") {
7
}
println ("seven is of type "+seven.getClass())
return seven
}
f()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment