Skip to content

Instantly share code, notes, and snippets.

@djk29a
Last active October 4, 2016 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djk29a/4a393b2633885ab01bee to your computer and use it in GitHub Desktop.
Save djk29a/4a393b2633885ab01bee to your computer and use it in GitHub Desktop.
Simple Scala timer I haven't tried to use since Scala 2.08
object Timer {
def time[R](block: => R): Pair[Long,_] = {
val t0 = System.nanoTime()
val result = block
val diff = System.nanoTime() - t0
// Post-timing processing can be added here, maybe
// an optional argument to this method?
Pair(diff, result)
}
}
defined module Timer
scala> val result = Timer.time { Thread.sleep(1234); 34 }
result: (Long, Any) = (1235042000,34)
@djk29a
Copy link
Author

djk29a commented Sep 30, 2016

Timer object can accept a block and return the block. No need for annotations ala AOP style code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment