Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Last active April 26, 2022 06:21
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kristopherjohnson/6009597 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/6009597 to your computer and use it in GitHub Desktop.
Class for measuring elapsed time in an Android app
package net.kristopherjohnson.util;
import android.os.SystemClock;
/**
* Measures elapsed time in milliseconds
*/
public class Stopwatch {
private long startThreadMillis;
private long startRealtimeMillis;
private long startUptimeMillis;
/**
* Result of Stopwatch.getElapsedTime()
*/
public static class ElapsedTime {
private final long elapsedThreadMillis;
private final long elapsedRealtimeMillis;
private final long elapsedUptimeMillis;
/**
* Constructor
*
* @param stopwatch
* instance from which to calculate elapsed time
*/
public ElapsedTime(Stopwatch stopwatch) {
elapsedThreadMillis = SystemClock.currentThreadTimeMillis() - stopwatch.startThreadMillis;
elapsedRealtimeMillis = SystemClock.elapsedRealtime() - stopwatch.startRealtimeMillis;
elapsedUptimeMillis = SystemClock.uptimeMillis() - stopwatch.startUptimeMillis;
}
/**
* 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
*/
public long getElapsedThreadMillis() {
return elapsedThreadMillis;
}
/**
* Get elapsed milliseconds, including time spent in sleep
*
* @return milliseconds
*/
public long getElapsedRealtimeMillis() {
return elapsedRealtimeMillis;
}
/**
* Get elapsed milliseconds, not counting time spent in deep sleep
*
* @return milliseconds
*/
public long getElapsedUptimeMillis() {
return elapsedUptimeMillis;
}
@Override
public String toString() {
return "realtime: " + elapsedRealtimeMillis
+ " ms; uptime: " + elapsedUptimeMillis
+ " ms; thread: " + elapsedThreadMillis + " ms";
}
}
/**
* Constructor
*/
public Stopwatch() {
reset();
}
/**
* Set stopwatch's start time to the current time
*/
public void reset() {
startThreadMillis = SystemClock.currentThreadTimeMillis();
startRealtimeMillis = SystemClock.elapsedRealtime();
startUptimeMillis = SystemClock.uptimeMillis();
}
/**
* Get elapsed time since construction or last call to reset()
*
* @return Stopwatch.ElapsedTime
*/
public ElapsedTime getElapsedTime() {
return new ElapsedTime(this);
}
/**
* 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
*/
public String getElapsedTimeString() {
double seconds = (double)getElapsedTime().getElapsedRealtimeMillis() / 1000.0;
if (seconds < 1.0) {
return String.format("%.0f ms", seconds * 1000);
}
else {
return String.format("%.2f s", seconds);
}
}
@Override
public String toString() {
return "Stopwatch: " + getElapsedTimeString();
}
}
@kristopherjohnson
Copy link
Author

Example usage:

Stopwatch stopwatch = new Stopwatch();
doSomethingThatTakesALongTime();
Log.d(LOGTAG, "elapsed time: " + stopwatch.getElapsedTimeString());

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