介绍
性能优化中经常会遇到要计算某个方法的耗时,通过一些工具如traceview的确可以分析大概的性能瓶颈,但是它们分析出的耗时大都是经过一定程度放大过的,要知道真正的耗时还是需要打印log
这里总结了一个工具类TimeRecoder,可以方便的打印耗时log
它主要做到三件事
1 |
|
其中begin和end中的字符串是惟一标识,会在打印log时输出如:1
Demo MainActivity#onCreate time spent=137ms
这些log代码可以留在release的代码中,可以随时关闭:1
TimeRecorder.setDebug(false);
如果不想写开关,这里还预留了一种adb的方式打开关闭log的方法:1
2
3
4#开启
$: adb shell setprop log.tag.time_recorder_log V
#关闭
$: adb shell setprop log.tag.time_recorder_log D
关于这种使用原理可参考: http://blog.csdn.net/qqxiaoqiang1573/article/details/72867776
//未完
代码
1 |
|