Cosider example for scala time funciton measuring:
def time[R](block: => R): R = {
val t0 = System.nanoTime()
val result = block // call-by-name
val t1 = System.nanoTime()
println("Elapsed time: " + (t1 - t0) + "ns")
result
}
Source taken from here.
Now try this code:
object MapTimeMeasure {
def main(args: Array[String]): Unit = {
val intToString: Int => String = x => s"$x"
val intToString2: Int => String = time (intToString)
}
}
And you will see Elapsed time
in the logs. So this time function can be used only for measuring time invocation "right now". But how to measure time for function without invocation? E.g. time
just wraps function and executes it only when wrapped funciton is called not when time
invocated itself. How to do this in scala?