KT-27856 Add contracts to Timing.kt lambdas (#1987)

This commit is contained in:
Tsvetan
2020-01-21 13:42:21 +01:00
committed by ilya-g
parent 7dc29a1840
commit 5f0e3018db
@@ -6,10 +6,15 @@
@file:kotlin.jvm.JvmName("TimingKt") @file:kotlin.jvm.JvmName("TimingKt")
package kotlin.system package kotlin.system
import kotlin.contracts.*
/** /**
* Executes the given [block] and returns elapsed time in milliseconds. * Executes the given [block] and returns elapsed time in milliseconds.
*/ */
public inline fun measureTimeMillis(block: () -> Unit): Long { public inline fun measureTimeMillis(block: () -> Unit): Long {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
val start = System.currentTimeMillis() val start = System.currentTimeMillis()
block() block()
return System.currentTimeMillis() - start return System.currentTimeMillis() - start
@@ -19,6 +24,9 @@ public inline fun measureTimeMillis(block: () -> Unit): Long {
* Executes the given [block] and returns elapsed time in nanoseconds. * Executes the given [block] and returns elapsed time in nanoseconds.
*/ */
public inline fun measureNanoTime(block: () -> Unit): Long { public inline fun measureNanoTime(block: () -> Unit): Long {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
val start = System.nanoTime() val start = System.nanoTime()
block() block()
return System.nanoTime() - start return System.nanoTime() - start