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