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() try { body() } finally { val end = Instant.now() return Duration.between(start, end) } }
}
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