Skip to content

Instantly share code, notes, and snippets.

@sorokod
Last active November 30, 2020 16:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sorokod/f5431b209e096f54e56c8c8ece2eb504 to your computer and use it in GitHub Desktop.
Save sorokod/f5431b209e096f54e56c8c8ece2eb504 to your computer and use it in GitHub Desktop.
class TimingLogger(
private val step: Long,
private val timeUnit: TimeUnit,
private val template: String,
private val counterScaler: Long = 1,
private val log: (String) -> Unit
) {
private var tickCounter: Long = 0L
private var timeMark = System.nanoTime()
fun tick() {
tickCounter++
if ((tickCounter % step) == 0L) {
val t = timeUnit.convert(System.nanoTime() - timeMark, NANOSECONDS)
val n = tickCounter / counterScaler
timeMark = System.nanoTime()
log(template.format(n, t))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment