Created
November 25, 2014 19:19
-
-
Save polymorphic/3993062a29b2ee0cb2fc to your computer and use it in GitHub Desktop.
Scala partially-applied function for measuring elapsed times
This file contains 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
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