衡量Android执行时间的最佳方法? [英] Best method to measure execution time in Android?
问题描述
衡量Android代码段执行时间的最佳方法是什么?
What is best method to measure execution time of Android code snippet?
我前后有一段代码,我想放置时间戳以了解其执行时间(例如,在 onCreate()
中一个,在 onDestroy()
中另一个>活动方法).
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).
我已经尝试过 Time.toMillies(false)
,但是它只会向我返回秒数(末尾带有恒定的 000
).我还尝试了两个Java函数: System.currentTimeMillis()
和 System.nanoTime()
.第一个返回纪元时间,以毫秒为单位.
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?
推荐答案
TimingLogger 怎么样?
来自 TimingLogger 文档:
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();
转储将如下所示:
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
不要忘记通过运行以下命令来启用标签: adb shell setprop log.tag.YOUR_TAG VERBOSE
Do not forget to enable your tag by running: adb shell setprop log.tag.YOUR_TAG VERBOSE
这篇关于衡量Android执行时间的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!