Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

java - Best method to measure execution time in Android?

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?

question from:https://stackoverflow.com/questions/23913762/best-method-to-measure-execution-time-in-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...