What does Kotlin's measureTime { } do, and how is it different from measureTimedValue { }?
answer
- measureTime -> Duration only
- measureTimedValue -> TimedValue<T>(value, duration)
- TimedValue is a destructurable data class
- Both are inline, in kotlin.time
- Built on a monotonic clock
basics
~10 smeasureTime runs a block and gives back how long it took as a Duration. measureTimedValue runs a block and gives back both the block's result and how long it took, packaged together.
solid answer
~40 sBoth are inline functions in kotlin.time used to time a block of code. measureTime { ... } executes the lambda and returns a Duration — the elapsed time only; the block's own return value is discarded. measureTimedValue { ... } returns a TimedValue<T>, a small data class with two properties: value (the block's result of type T) and duration (the elapsed Duration). Use measureTime when you only care about timing (e.g., logging how long something took); use measureTimedValue when you need the computed result AND its timing, so you don't have to compute or store it separately. Both rely on a monotonic clock under the hood, so they measure true elapsed time rather than wall-clock differences. Example: val (result, took) = measureTimedValue { expensiveCompute() }.
code
kotlin · 11 linesimport kotlin.time.measureTime
import kotlin.time.measureTimedValue
val onlyTime = measureTime { Thread.sleep(50) }
println(onlyTime) // ~50ms, no result
val timed = measureTimedValue { (1..1_000).sum() }
println(timed.value) // 500500
println(timed.duration) // e.g. 12.us
val (sum, took) = measureTimedValue { (1..1_000).sum() }go deeper
Knows measureTime returns a Duration and measureTimedValue also returns the result.
Explains TimedValue<T> fields, destructuring, and when to pick each function.
Notes inline nature, the kotlin.time package, and the monotonic clock backing both.
Frames these as the idiomatic replacement for ad-hoc System.nanoTime() timing and discusses Duration as a first-class measurement type.
## What these functions are `measureTime` and `measureTimedValue` are top-level **inline** functions in the `kotlin.time` package. They time how long a block of code takes to run. - **Duration** — a Kotlin type representing an amount of elapsed time (e.g. `1.5.seconds`, `300.milliseconds`). It is not a clock reading; it is a length of time. - **inline** — the compiler copies the function body into the call site, so there is essentially no overhead from the lambda and no extra object allocation just to time the block. ## measureTime ```kotlin import kotlin.time.measureTime val elapsed: Duration = measureTime { doSomeWork() // the lambda's own return value is ignored } println(elapsed) // e.g. 42.3ms ``` It runs the lambda, returns the elapsed `Duration`, and **throws away** whatever the lambda returned. Use it when you only want the timing. ## measureTimedValue ```kotlin import kotlin.time.measureTimedValue val timed = measureTimedValue { computeAnswer() } println(timed.value) // the computeAnswer() result println(timed.duration) // how long it took, a Duration ``` It returns a **`TimedValue<T>`** — a small `data class` holding: - `value: T` — the result of the block - `duration: Duration` — the elapsed time Because it is a `data class`, you can destructure it: ```kotlin val (answer, took) = measureTimedValue { computeAnswer() } ``` ## Why two functions If you used `measureTime` but also needed the result, you would have to store the result in a `var` from inside the lambda — awkward. `measureTimedValue` solves that by returning both together. Choose `measureTime` for pure timing, `measureTimedValue` when the result matters too. ## Monotonic clock Both measure against a **monotonic** time source (steadily increasing, immune to wall-clock adjustments), so daylight-saving changes or NTP corrections cannot make a measured duration negative or jumpy.
- What is the type returned by measureTimedValue?TimedValue<T>, a data class with a value: T property and a duration: Duration property; it can be destructured.
- Which package are these in?kotlin.time.
measureTime is a stopwatch that only tells you the time; measureTimedValue is a stopwatch that also hands you the thing you were timing yourself doing.
saying these in an interview costs you the question
- Saying measureTime returns the block's result (it returns a Duration)
- Thinking measureTimedValue returns a Long of milliseconds
- Claiming you must use System.currentTimeMillis() to get the result and timing
- Not knowing TimedValue can be destructured