gpt4 book ai didi

android - Android中的时间代码执行

转载 作者:IT老高 更新时间:2023-10-28 21:39:58 25 4
gpt4 key购买 nike

Android 中最容易执行的时间是什么?

我环顾四周,在 Android SDK 上找到了 TimingLogger,和说明here .看起来很方便。但我无法让它工作。这是我的代码:

TimingLogger timings = new TimingLogger("TopicLogTag", "Parsing html");
My code to time here...
timings.dumpToLog();

它应该在 LogCat 中转储时间。但我什么都看不到.. 我做错了什么? Eclipse 不显示任何 varnings。我猜它有一些详细的输出,但我已将 LogCat 设置为显示 Verbose。谢谢。。

最佳答案

我对其进行了试运行,我也遇到了同样的情况。这一切都归结为 Javadoc for TimingLogger 中的这一点描述。 :

If the Log.isLoggable is not enabled to at least the Log.VERBOSE level for that tag at creation time then the addSplit and dumpToLog call will do nothing.

我在本地做了一个测试:

TimingLogger timings = new TimingLogger("MyTag", "Initialization");
Log.d("MyTag", "Is Loggable? " + Log.isLoggable("MyTag", Log.VERBOSE));
timings.dumpToLog();

奇怪的是,我得到了日志的输出:

06-28 08:35:18.693: DEBUG/MyTag(24366): Is Loggable? false

但就是这样。由于它是错误的,我怀疑 TimingLogger 是否在做任何事情,基于 TimingLogger code :

  90     /**
91 * Clear and initialize a TimingLogger object that will log using
92 * the tag and label that was specified previously, either via
93 * the constructor or a call to reset(tag, label). If the
94 * Log.isLoggable is not enabled to at least the Log.VERBOSE
95 * level for that tag at creation time then the addSplit and
96 * dumpToLog call will do nothing.
97 */
98 public void reset() {
99 mDisabled = !Log.isLoggable(mTag, Log.VERBOSE);
100 if (mDisabled) return;
101 if (mSplits == null) {
102 mSplits = new ArrayList<Long>();
103 mSplitLabels = new ArrayList<String>();
104 } else {
105 mSplits.clear();
106 mSplitLabels.clear();
107 }
108 addSplit(null);
109 }

我不确定为什么 Log.isLoggable 显然在 VERBOSE 以上记录时返回 false,因为我的 Log.d 显然已记录。

您可以从 [Log class Javadoc][3] 手动启用该标记的日志记录:

You can change the default level by setting a system property: 'setprop log.tag. ' Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will turn off all logging for your tag. You can also create a local.prop file that with the following in it: 'log.tag.=' and place that in /data/local.prop.

我通过 adb shell 做的:

$ adb shell
# setprop
usage: setprop <key> <value>
# setprop log.tag.MyTag VERBOSE
#

结果:

06-28 08:53:42.447: DEBUG/MyTag(24739): Is Loggable? true
06-28 08:53:44.744: DEBUG/MyTag(24739): Initialization: begin
06-28 08:53:44.744: DEBUG/MyTag(24739): Initialization: end, 0 ms

查看 droidgren 对此答案的评论 - 显然调用 addSplit 也是必要的。

[3]:http://developer.android.com/reference/android/util/Log.html#isLoggable(java.lang.String , int)

关于android - Android中的时间代码执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3128181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com