Created
July 22, 2012 15:55
-
-
Save d6y/3160096 to your computer and use it in GitHub Desktop.
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
| 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