Lambda for benchmarking a function call
suggest changeGeneral-purpose stopwatch for timing how long a function takes to run:
object Benchmark {
fun realtime(body: () -> Unit): Duration {
val start = Instant.now()
body()
return Duration.between(start, Instant.now())
}
}
Usage:
val time = Benchmark.realtime({
// some long-running code goes here ...
})
println("Executed the code in $time")
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents