Skip to content

Instantly share code, notes, and snippets.

@yongjhih
Created March 9, 2023 03:02
Show Gist options
  • Select an option

  • Save yongjhih/d433938a485902a7c53c600743975e9b to your computer and use it in GitHub Desktop.

Select an option

Save yongjhih/d433938a485902a7c53c600743975e9b to your computer and use it in GitHub Desktop.
@OptIn(ExperimentalContracts::class)
fun <T: () -> R, R> T.measureTimeMillis(test: Boolean = true, onMeasure: (Long) -> Unit): R {
contract {
callsInPlace(this@measureTimeMillis, InvocationKind.EXACTLY_ONCE)
}
if (!test) return this()
val start = System.currentTimeMillis()
val res = this()
onMeasure(System.currentTimeMillis() - start)
return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment