Measuring Time

How much time an operation or sequence of operations takes to complete is a critical piece of information when it is time to optimize code. Without knowing how much time is spent doing something, your optimizations are impossible to measure. Java and Android provide the following simple APIs your application can use to measure time and therefore performance:

  • System.currentTimeMillis
  • System.nanoTime
  • Debug.threadCpuTimeNanos
  • SystemClock.currentThreadTimeMillis
  • SystemClock.elapsedRealtime
  • SystemClock.uptimeMillis

Typically, your application needs to make two calls to these methods as a single call is hardly meaningful. To measure time, your application needs a start time and an end time, and performance is measured as the difference ...

Get Pro Android Apps Performance Optimization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.