Skip to content

Instantly share code, notes, and snippets.

@anoopmaddasseri
Last active July 26, 2024 10:47
Show Gist options
  • Save anoopmaddasseri/6ae01dafe6626a6de26d907981a02d9e to your computer and use it in GitHub Desktop.
Save anoopmaddasseri/6ae01dafe6626a6de26d907981a02d9e to your computer and use it in GitHub Desktop.
Kotlin Snippet: Benchmark elapsed execution time
package com.intigral.jawwytv.util.test
import android.os.SystemClock
/**
* Measures elapsed time in milliseconds
*/
class Stopwatch {
private var startThreadMillis: Long = 0
private var startRealtimeMillis: Long = 0
private var startUptimeMillis: Long = 0
/**
* Result of Stopwatch.getElapsedTime()
*/
class ElapsedTime(stopwatch: Stopwatch) {
/**
* Get milliseconds running in current thread
*
*
* This result is only valid if Stopwatch.getElapsedTime() is called from the same
* thread as the Stopwatch constructor, or the last call to Stopwatch.reset().
*
* @return milliseconds
*/
val elapsedThreadMillis: Long
/**
* Get elapsed milliseconds, including time spent in sleep
*
* @return milliseconds
*/
val elapsedRealtimeMillis: Long
/**
* Get elapsed milliseconds, not counting time spent in deep sleep
*
* @return milliseconds
*/
val elapsedUptimeMillis: Long
/**
* Constructor
*
* @param stopwatch instance from which to calculate elapsed time
*/
init {
elapsedThreadMillis =
SystemClock.currentThreadTimeMillis() - stopwatch.startThreadMillis
elapsedRealtimeMillis = SystemClock.elapsedRealtime() - stopwatch.startRealtimeMillis
elapsedUptimeMillis = SystemClock.uptimeMillis() - stopwatch.startUptimeMillis
}
override fun toString(): String {
return "realtime: $elapsedRealtimeMillis ms; uptime: $elapsedUptimeMillis ms; thread: $elapsedThreadMillis ms"
}
}
/**
* Constructor
*/
init {
reset()
}
/**
* Set stopwatch's start time to the current time
*/
fun reset() {
startThreadMillis = SystemClock.currentThreadTimeMillis()
startRealtimeMillis = SystemClock.elapsedRealtime()
startUptimeMillis = SystemClock.uptimeMillis()
}
val elapsedTime: ElapsedTime
/**
* Get elapsed time since construction or last call to reset()
*
* @return Stopwatch.ElapsedTime
*/
get() = ElapsedTime(this)
val elapsedTimeString: String
/**
* Get elapsed time as a human-readable string
*
*
* If time is less than one second, it will be rendered as a number of milliseconds.
* Otherwise, it will be rendered as a number of seconds.
*
* @return String
*/
get() {
val seconds = elapsedTime.elapsedRealtimeMillis.toDouble() / 1000.0
return if (seconds < 1.0) {
String.format("%.0f ms", seconds * 1000)
} else {
String.format("%.2f s", seconds)
}
}
override fun toString(): String {
return "Stopwatch: $elapsedTimeString"
}
}
@anoopmaddasseri
Copy link
Author

anoopmaddasseri commented Jul 26, 2024

Usage:

val stopwatch = Stopwatch()
block()
val elapsedTimeStr = stopwatch.elapsedTimeString
Timber.d("measureExecution: $elapsedTimeStr")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment