Skip to content

Instantly share code, notes, and snippets.

@polymorphic
Created November 25, 2014 19:19
Show Gist options
  • Save polymorphic/3993062a29b2ee0cb2fc to your computer and use it in GitHub Desktop.
Save polymorphic/3993062a29b2ee0cb2fc to your computer and use it in GitHub Desktop.
Scala partially-applied function for measuring elapsed times
import scala.concurrent.duration._
object Measurements {
/*
Partially-aplied function to measure elapsed times.
Usage:
val myTimer = buildTimer // start the timer
// code under measurement
val elapsedTime1 = myTimer() // record elapsed time 1, leave the timer running
// more code
val elapsedTime2 = myTimer() // record elapsed time 2, leave the timer running
*/
def buildNanoTimer: PartialFunction[Unit, Duration] = {
val t0 = System.nanoTime()
val inner: PartialFunction[Unit, Duration] = { case _: Unit => Duration(System.nanoTime() - t0, NANOSECONDS)}
inner
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment