2

Kotlin - calculate time elapsed

 2 years ago
source link: http://mussatto.github.io/kotlin/time/elapsed/2020/08/11/kotlin-measure-elapsed.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

“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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK