Best method to measure execution time in Android?

android, java

Solution

What about TimingLogger?

From TimingLogger documentation:

TimingLogger timings = new TimingLogger(YOUR_TAG, "methodA");
// ... do some work A ... 
timings.addSplit("work A");
// ... do some work B ... 
timings.addSplit("work B");
// ... do some work C ... 
timings.addSplit("work C");
timings.dumpToLog();

and the dump will look like:

     D/TAG     (3459): methodA: begin
     D/TAG     (3459): methodA:      9 ms, work A
     D/TAG     (3459): methodA:      1 ms, work B
     D/TAG     (3459): methodA:      6 ms, work C
     D/TAG     (3459): methodA: end, 16 ms

Do not forget to enable your tag by running: `adb shell setprop log.tag.YOUR_TAG VERBOSE`

Problem

What is best method to measure execution time of Android code snippet? I have a section of code before and after which I want to place timestamps to find out it's execution time (e.g. one in `onCreate()` and another in `onDestroy()` method of activity). I have tried `Time.toMillies(false)`, but it only returns seconds to me (with constant `000` at the end). I also tried two java functions: `System.currentTimeMillis()` and `System.nanoTime()`. First one returns milliseconds of epoch time, second doesn't. What would be the best way to measure execution time and get good precision?

Original source