Skip to content

Instantly share code, notes, and snippets.

@autovalue
Last active January 17, 2016 02:01
Show Gist options
  • Save autovalue/be0016d0654c234ee6ea to your computer and use it in GitHub Desktop.
Save autovalue/be0016d0654c234ee6ea to your computer and use it in GitHub Desktop.
Android Java equivalent of JavaScript's console.time function
import android.util.Log;
import java.util.HashMap;
public class Timer {
private static HashMap<String, Long> measurements = new HashMap<>();
private Timer(){}
public static void start(String benchmark){
measurements.put(benchmark, System.currentTimeMillis());
}
public static long end(String benchmark) {
if(measurements.containsKey(benchmark)) {
long startTime = measurements.remove(benchmark);
long elapsedTime = System.currentTimeMillis() - startTime;
Log.d("MethodTimer", "[" + benchmark + "] Elapsed time: " + elapsedTime + "ms.");
return elapsedTime;
}
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment