Java 在Android中测量执行时间的最佳方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23913762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best method to measure execution time in Android?
提问by 22332112
What is best method to measure execution time of Android code snippet?
测量Android代码片段执行时间的最佳方法是什么?
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).
我有一段代码之前和之后,我想放置时间戳以找出它的执行时间(例如,一个在活动方法中onCreate()
,另一个在onDestroy()
活动方法中)。
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.
我试过Time.toMillies(false)
,但它只返回几秒钟给我(最后是常数000
)。我还尝试了两个 java 函数:System.currentTimeMillis()
和System.nanoTime()
. 第一个返回纪元时间的毫秒数,第二个不返回。
What would be the best way to measure execution time and get good precision?
测量执行时间并获得良好精度的最佳方法是什么?
回答by Marcin Orlowski
What you ask about is called profiling, and there're tools to help you with that on Android as well. See article Profiling with Traceview and dmtracedumpon official developer site.
您询问的内容称为profiling,并且在 Android 上也有一些工具可以帮助您解决此问题。请参阅官方开发人员站点上的文章使用 Traceview 和 dmtracedump进行分析。
回答by CommonsWare
What would be the best way to measure execution time
衡量执行时间的最佳方法是什么
System.nanoTime()
is probably a good choice. Jake Wharton is using that with Hugo, for example.
System.nanoTime()
大概是个不错的选择。例如,Hyman·沃顿 (Jake Wharton) 正在将其与Hugo一起使用。
and get good precision
并获得良好的精度
This is not strictly possible, as anything can happen on the device while your method is executing. Those external factors will affect your time measurements, by stealing away CPU time, tying up I/O channels, etc. You need to average your tests across several runs to try to average out those external factors, and accuracy/precision will suffer as a result.
这在严格意义上是不可能的,因为当您的方法正在执行时,设备上可能会发生任何事情。这些外部因素会通过窃取 CPU 时间、占用 I/O 通道等影响您的时间测量。您需要在多次运行中平均您的测试以尝试平均这些外部因素,并且准确性/精度将受到影响结果。
And, as Marcin Orlowski notes, to actually figure out whyyou are consuming certain amounts of time, use Traceview.
而且,正如 Marcin Orlowski 指出的那样,要真正弄清楚为什么要消耗一定数量的时间,请使用 Traceview。
回答by MikeL
What about TimingLogger?
什么TimingLogger?
From TimingLoggerdocumentation:
从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();
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
不要忘记通过运行来启用您的标签: adb shell setprop log.tag.YOUR_TAG VERBOSE
回答by Hitesh Sahu
I usually use System.nanoTime()
for quick Measurement.
我通常 System.nanoTime()
用于快速测量。
A simple Setup like this
像这样的简单设置
val startTime = System.nanoTime()
//DO SOMETHING
Log.e("Measure", TASK took : " + ((System.nanoTime()-startTime)/1000000)+ "mS\n")