Skip to content

Instantly share code, notes, and snippets.

@jgabrielfreitas
Last active August 3, 2020 02:07
Show Gist options
  • Select an option

  • Save jgabrielfreitas/97e77334820bfc499b9f89a57e6ac386 to your computer and use it in GitHub Desktop.

Select an option

Save jgabrielfreitas/97e77334820bfc499b9f89a57e6ac386 to your computer and use it in GitHub Desktop.
Simple stopwatch in kotlin
object SimpleStopwatch {
    suspend fun <T> runWithStopWatch(log: Logger, tag: String, block: suspend () -> T): T {
        val start = System.currentTimeMillis()
        val result = block()
        val executionTime = System.currentTimeMillis() - start
        log.info("=======> $tag took ${executionTime}ms to finish")
        return result
    }
}

usage

val savedUser: User = runWithStopWatch(log, "saving-user") { userDataAccessPort.save(user) }
// =======> saving-user took 2ms to finish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment