Anybody know if there’s some way to do `TimeMark.e...
# announcements
Anybody know if there’s some way to do
TimeMark.elapsedSince(other: TimeMark)
? or plans to add it?
To get a Duration?
Otherwise can help in tickMark += 8.hours
👍 2
i’m basically trying to do this:
Copy code
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
for both of those but then the values are slightly different between the two calls
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?
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()
timer.log("eating tacos")
makes it easier to see which parts of a method take the most time
You can do
lastEvent + elapsedSinceLast
to get a new
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
well that’s lovely, thank you jake
had not considered
lastMark + elapsed
as the way to update the mark