measureNanoTime

inline fun measureNanoTime(block: () -> Unit): Long(source)

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.0

See 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
}
inline fun measureNanoTime(block: () -> Unit): Long(source)

Deprecated

Warning since 1.9

Error since 2.1

Use measureTime() instead.

Replace with

import kotlin.time.measureTime
measureTime(block).inWholeNanoseconds

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.3

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
}