Hello, I’m trying to measure the time it takes to ...
# server
a
Hello, I’m trying to measure the time it takes to execute a function although I want to log the time it took even if the function throws any exception. I came up with this approach but seems ugly to me. What you guys think? Is there any other approach?
Copy code
val (result, duration) = measureTimedValue {
    try {
        foo()
    } catch (ex: Throwable) {
        ex
    }
}
when(result) {
    is Throwable -> println("Error ${duration.inWholeMilliseconds} ms")
    else -> println("Success ${duration.inWholeMilliseconds} ms")
}
h
What about
tryCatching
and
Result
?
a
You mean something like this?
Copy code
val (result, duration) = measureTimedValue {
    runCatching { foo() }
}

if (result.isSuccess) {
    println("Success ${duration.inWholeMilliseconds} ms")
} else {
    println("Error ${duration.inWholeMilliseconds} ms")
}
Ya I think looks better 😛