implementation 'in.xiandan:anycost:1.0.5'
AnyCost.begin(key)
long end = AnyCost.end(key)
AnyCost.getInstance().enable(BuildConfig.DEBUG)
// 添加监听
AnyCost.getInstance().addOnTimingListener(new OnTimingListener() {
@Override
public void onTimingBegin(String key, String threadName) {
}
@Override
public void onTimingEnd(String key, String threadName, long time, Object extras) {
}
});
//移除监听
AnyCost.getInstance().removeOnTimingEndListener(listener)
//移除所有监听
AnyCost.getInstance().removeOnTimingEndListener()
如果需要通过注解自动计时,还需要启用插件
buildscript {
dependencies {
classpath 'in.xiandan:anycost-gradle-plugin:1.0.1'
}
}
app
-build.gradle
plugins {
id 'anycost'
}
在需要计算耗时的方法加上AnyCostMark
注解
@AnyCostMark(key = "test")
private void test() {
}
cost(key) {
// 自动计算此代码块所耗的时间
}
AnyCost.getInstance().addOnTimingListener { key, threadName, time, extras ->
}