measureNanoTime

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

Executes the given block and returns elapsed time in nanoseconds.

Since Kotlin

1.0

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)

Executes the given block and returns elapsed time in nanoseconds.

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
}