Last active
February 17, 2019 01:45
-
-
Save psteiger/de1b1e61f86d92097971d6f9e3bfca08 to your computer and use it in GitHub Desktop.
Kotlin - A wrapper for measureTimeMillis that also prints to ADB log.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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