measureNanoTime
For JVM
Executes the given block and returns elapsed time in nanoseconds.
import kotlin.system.*
fun main(args: Array<String>) {
//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
}
For Native
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.
import kotlin.system.*
fun main(args: Array<String>) {
//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
}