Kotlin - calculate time elapsed
“Stuff I always forget how to do and have to google it”
Calculate elapsed in millis in kotlin:
val elapsed = measureTimeMillis {
Thread.sleep(1000L)
}
assertThat(elapsed).isGreaterThan(1000L)
Calculate elapsed in seconds, nano seconds, milliseconds:
val elapsed = measureTime {
Thread.sleep(1100L)
}
assertThat(elapsed.inSeconds).isGreaterThan(1.0)
println("micro=${elapsed.inMicroseconds}, nano=${elapsed.inNanoseconds}, milli=${elapsed.inMilliseconds}")
Obs: Needs @ExperimentalTime annotation
Source code: Here