Skip to content

Instantly share code, notes, and snippets.

@psteiger
Last active February 17, 2019 01:45
Show Gist options
  • Save psteiger/de1b1e61f86d92097971d6f9e3bfca08 to your computer and use it in GitHub Desktop.
Save psteiger/de1b1e61f86d92097971d6f9e3bfca08 to your computer and use it in GitHub Desktop.
Kotlin - A wrapper for measureTimeMillis that also prints to ADB log.
// Usage: logTime { anyFunction() }
// Output in Android logcat: LogTime: 2ms
// logTime returns the value and type returned by the function, so you can:
//
// val distance: Int = logTime { distanceTo.localized }
//
// It will print to console log the time `distanceTo.localized` took to process,
// and will return its value to be captured by `distance` var.
fun <T> logTime(tag: String = "LogTime", f: () -> T): T {
val result: T by lazy { f() }
Log.d(tag, "${measureTimeMillis { result }}")
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment