Last active
August 29, 2015 13:57
-
-
Save pedrofurla/9885465 to your computer and use it in GitHub Desktop.
A little combinator to measure the time of side effects. YES, *side effects* for now
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
| object Chronograph { | |
| /** Chronon is the quantum of time, here in our silly computers it got be a Long */ | |
| type Chronon = Long | |
| sealed case class TimeUnit(chrononsInSecond: Chronon, name:String, getTime: => Chronon) | |
| val Nanos = new TimeUnit(1e9.toLong, "ns", System.nanoTime) | |
| val Micros = new TimeUnit(1e6.toLong, "μs", System.nanoTime*1000) | |
| val Millis = new TimeUnit(1e3.toLong, "ms", System.currentTimeMillis()) | |
| type EllapsedTimeOf[A] = (A, Chronon, TimeUnit) | |
| /** Records the measures of time of the `f` computation */ | |
| def chronograph[A,B](timer: TimeUnit)(f: (A => B)): A => EllapsedTimeOf[B] = | |
| a => chronometer(timer)(f(a)) | |
| /** Measures times of the `u` computation*/ | |
| def chronometer[A,B](timer: TimeUnit)(u: => B):EllapsedTimeOf[B] = { | |
| val start = timer.getTime | |
| val res = u | |
| val end = timer.getTime | |
| (u,end - start, timer) | |
| } | |
| def nanoChronograph[A,B](f:A => B): A => EllapsedTimeOf[B] = chronograph(Nanos)(f) | |
| def microChronograph[A,B](f:A => B): A => EllapsedTimeOf[B] = chronograph(Micros)(f) | |
| def millisChronograph[A,B](f:A => B): A => EllapsedTimeOf[B] = chronograph(Millis)(f) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment