measureTimedValue
Executes the given function block and returns an instance of TimedValue class, containing both the result of the function execution and the duration of the elapsed time interval.
The elapsed time is measured with TimeSource.Monotonic which is the most precise available time source on the platform.
Since Kotlin
1.9Samples
import kotlin.test.Test
import kotlin.time.Duration.Companion.seconds
import kotlin.time.*
fun main() {
//sampleStart
fun slowFunction(): Unit = Thread.sleep(1000L)
val result = measureTimedValue {
slowFunction()
42
}
println("Computed result: ${result.value}, time elapsed: ${result.duration}")
//sampleEnd
}
Executes the given function block and returns an instance of TimedValue class, containing both the result of function execution and the duration of the elapsed time interval.
The elapsed time is measured with the specified this
TimeSource instance.
Since Kotlin
1.9Samples
import kotlin.test.Test
import kotlin.time.Duration.Companion.seconds
import kotlin.time.*
fun main() {
//sampleStart
val testSource = TestTimeSource()
val result = testSource.measureTimedValue {
println("Pretending this function executes 10 seconds")
testSource += 10.seconds
42
}
println("Computed result: ${result.value}, time elapsed: ${result.duration}")
//sampleEnd
}
Executes the given function block and returns an instance of TimedValue class, containing both the result of function execution and the duration of the elapsed time interval.
The elapsed time is measured with the specified this
TimeSource.Monotonic instance. The explicit instance allows using non-boxed version of TimeSource.Monotonic.ValueTimeMark and, optionally, be more explicit about the intent.
Since Kotlin
1.9Samples
import kotlin.test.Test
import kotlin.time.Duration.Companion.seconds
import kotlin.time.*
fun main() {
//sampleStart
fun slowFunction(): Unit = Thread.sleep(1000L)
val result = TimeSource.Monotonic.measureTimedValue() {
slowFunction()
42
}
println("Computed result: ${result.value}, time elapsed: ${result.duration}")
//sampleEnd
}