advent-of-code/2019/Scala/kamlib/Wrapper.scala

30 lines
708 B
Scala
Raw Normal View History

2019-12-06 22:10:37 +01:00
package kamlib
/** Wraps a function, measures the execution time, provides the result and time*/
class Wrapper[A](function: => A)
{
/** Saved results */
val tuple: (A, Int) = wrap(function)
/** Time measurement */
private[this] def wrap[A](function: => A): (A, Int) =
{
val evalStart = System.currentTimeMillis()
val result = function
val evalEnd = System.currentTimeMillis()
(result, (evalEnd-evalStart).toInt)
}
def print(): Unit =
{
println(s"Result: ${tuple._1} | Time: ${tuple._2}ms")
}
def result: A = tuple._1
def time: Int = tuple._2
}
object Wrapper
{
def apply[A](function: => A): Wrapper[A] = new Wrapper[A](function)
}