Anybody know if there’s some way to do `TimeMark.e...
# announcements
e
Anybody know if there’s some way to do
TimeMark.elapsedSince(other: TimeMark)
? or plans to add it?
j
To get a Duration?
Otherwise can help in tickMark += 8.hours
👍 2
e
i’m basically trying to do this:
Copy code
@Suppress("unused")
class DebugTimer(private val logger: (String) -> Unit) {
  private var firstEvent = TimeSource.Monotonic.markNow()
  private var lastEvent = firstEvent
  private var isFirstEvent = true

  fun log(message: String) {
    val now = TimeSource.Monotonic.markNow()
    val elapsedSinceLast = lastEvent.elapsedSince(now)
    val elapsedSinceFirst = if (isFirstEvent) null else firstEvent.elapsedSince(now)
    val totalSuffix = elapsedSinceFirst?.let { "(total ${elapsedSinceFirst})" } ?: ""
    logger.invoke("DTimer: $message took $elapsedSinceLast $totalSuffix")
    lastEvent = now
    isFirstEvent = false
  }
}
i can do
elapsedNow()
for both of those but then the values are slightly different between the two calls
n
What are you trying to accomplish? Comparing or totaling execution times for code blocks? Would
val tv1 = measureTimedValue { block1 }; val tv2 = measureTimedValues { block2 }
not do what you want?
e
yeah, i just don’t want to introduce blocks for everything i’m measuring, i like being able to do this:
Copy code
val timer = DebugTimer.start()
buildThing()
timer.log("building")
eatTacos()
timer.log("eating tacos")
makes it easier to see which parts of a method take the most time
j
You can do
lastEvent + elapsedSinceLast
to get a new
TimeMark
for when you measured
oh wait your code isn't real
but it's still what you want. use elapsedNow() to get a Duration that holds the distance since the last mark, then add that distance to the mark to get your measured "now" to use as the new lastEvent time
let me write it up
i would rename lastNow to lastMark
e
well that’s lovely, thank you jake
had not considered
lastMark + elapsed
as the way to update the mark