org.pkl.thirdparty.kotlin.time.measureTime.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pkl-tools Show documentation
Show all versions of pkl-tools Show documentation
Fat Jar containing pkl-cli, pkl-codegen-java, pkl-codegen-kotlin, pkl-config-java, pkl-core, pkl-doc, and their shaded third-party dependencies.
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.pkl.thirdparty.kotlin.time
import org.pkl.thirdparty.kotlin.contracts.*
/**
* Executes the given function [block] and returns the duration of elapsed time interval.
*
* The elapsed time is measured with [TimeSource.Monotonic].
*/
@SinceKotlin("1.3")
@ExperimentalTime
public inline fun measureTime(block: () -> Unit): Duration {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return TimeSource.Monotonic.measureTime(block)
}
/**
* Executes the given function [block] and returns the duration of elapsed time interval.
*
* The elapsed time is measured with the specified `this` [TimeSource] instance.
*/
@SinceKotlin("1.3")
@ExperimentalTime
public inline fun TimeSource.measureTime(block: () -> Unit): Duration {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
val mark = markNow()
block()
return mark.elapsedNow()
}
/**
* Executes the given function [block] and returns the duration of elapsed time interval.
*
* The elapsed time is measured with the specified `this` [TimeSource.Monotonic] instance.
*/
@SinceKotlin("1.7")
@ExperimentalTime
public inline fun TimeSource.Monotonic.measureTime(block: () -> Unit): Duration {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
val mark = markNow()
block()
return mark.elapsedNow()
}
/**
* Data class representing a result of executing an action, along with the duration of elapsed time interval.
*
* @property value the result of the action.
* @property duration the time elapsed to execute the action.
*/
@SinceKotlin("1.3")
@ExperimentalTime
public data class TimedValue(val value: T, val duration: Duration)
/**
* Executes the given function [block] and returns an instance of [TimedValue] class, containing both
* the result of the function execution and the duration of elapsed time interval.
*
* The elapsed time is measured with [TimeSource.Monotonic].
*/
@SinceKotlin("1.3")
@ExperimentalTime
public inline fun measureTimedValue(block: () -> T): TimedValue {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return TimeSource.Monotonic.measureTimedValue(block)
}
/**
* Executes the given [block] and returns an instance of [TimedValue] class, containing both
* the result of function execution and the duration of elapsed time interval.
*
* The elapsed time is measured with the specified `this` [TimeSource] instance.
*/
@SinceKotlin("1.3")
@ExperimentalTime
public inline fun TimeSource.measureTimedValue(block: () -> T): TimedValue {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
val mark = markNow()
val result = block()
return TimedValue(result, mark.elapsedNow())
}
/**
* Executes the given [block] and returns an instance of [TimedValue] class, containing both
* the result of function execution and the duration of elapsed time interval.
*
* The elapsed time is measured with the specified `this` [TimeSource.Monotonic] instance.
*/
@SinceKotlin("1.7")
@ExperimentalTime
public inline fun TimeSource.Monotonic.measureTimedValue(block: () -> T): TimedValue {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
val mark = markNow()
val result = block()
return TimedValue(result, mark.elapsedNow())
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy