Lambda for benchmarking a function call

suggest change

General-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")

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents