Time Measure
measureTime, measureTimeWithResult & PerformanceCounter
Document not reviewed yet, might be outdated. Please,
let us know
if you find something invalid here.
On this page
Klock has utilities for mesuring time.
Measuring Time
As for Klock 1.0, there are two relevant functionality: the measureTime
, measureTimeWithResult
functions and the PerformanceCounter
class.
measureTime
This function is inline and allocation-free, and can be used for expensive computations as well as for asynchronous blocks:
val time: TimeSpan = measureTime {
// expensive or asynchronous computation
}
measureTimeWithResult
This function is inline but it allocates a TimedResult instance, so it is not suitable for critical places, but allows to return a result along the time:
val timedResult: TimedResult<String> = measureTimeWithResult {
// expensive or asynchronous computation
"result"
}
val time: TimeSpan = timedResult.time
val result: String = timedResult.result
PerformanceCounter
This class offers a performance counter that will increase over time but that cannot be used as reference in time. Only can be used as relative time to compute deltas:
val start: Double = PerformanceCounter.microseconds
// ...
val end: Double = PerformanceCounter.microseconds
val elapsed: TimeSpan = (end - start).microseconds