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()
        body()
        return Duration.between(start, Instant.now())
    }
}

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