measureNanoTime
Executes the given block and returns elapsed time in nanoseconds. For a more human-readable and typed output, measureTime can be used instead.
Since Kotlin
1.0See also
Samples
import kotlin.system.*
fun main() {
//sampleStart
var sqrt = 0
val number = 1000
val timeInNanos = measureNanoTime {
while (sqrt * sqrt < number) sqrt++
}
println("(The operation took $timeInNanos ns)")
println("The approximate square root of $number is between ${sqrt - 1} and $sqrt")
//sampleEnd
}
Deprecated
Warning since 1.9
Error since 2.1
Use measureTime() instead.
Replace with
import kotlin.time.measureTime
Content copied to clipboard
measureTime(block).inWholeNanoseconds
Content copied to clipboard
Executes the given block and returns elapsed time in nanoseconds.
This function is deprecated. To measure the duration of execution of a block of code, use measureTime or measureTimedValue instead. The resulting Duration then can be expressed as a Long number of nanoseconds using Duration.inWholeNanoseconds.
Since Kotlin
1.3Samples
import kotlin.system.*
fun main() {
//sampleStart
var sqrt = 0
val number = 1000
val timeInNanos = measureNanoTime {
while (sqrt * sqrt < number) sqrt++
}
println("(The operation took $timeInNanos ns)")
println("The approximate square root of $number is between ${sqrt - 1} and $sqrt")
//sampleEnd
}