Skip to Content

TimingLogger

TimingLogger

Sometimes developers needs to analyze performance of a small friction of code. Maybe a RecyclerView is not scrolling smoothly or a calculation needs too much time, we have to split it up and try to find the line or block that runs inefficently. We all know to get a smooth user experience we only have 16ms for each frame to render. So it is very important to locate and optimize inefficient code.

In this cases TimingLogger class is our friend.

val logger = TimingLogger(„PerformanceCheck", „democheck“)
//do something
logger.addSplit („calculated $var1“)
//do something else 
logger.addSplit („calculated $var2“)
//
// add as much addSplit as you need for your use case
//
logger.dumpToLog()

The dumpToLog call will produce the following log output:

 D/Logger: values: begin 
 D/Logger: democheck:      14 ms, calculated var1Value
 D/Logger: democheck:       2 ms, calculated var2Value
 D/Logger: democheck: end, 1131 ms

IMPORTANT: Enable the log output for this class with the following adb command:

adb shell setprop log.tag.PerformanceCheck VERBOSE

PerformanceCheck is the first parameter when creating a new TimingLogger object

ProTip: Strip out TimingLogger calls from code in production builds use ProGuard

-assumenosideeffects class android.util.TimingLogger {
    public void addSplit(java.lang.String))
    public void dumpToLog();
    public void reset()
    public void reset(java.lang.String, java.lang.String)
}

TimingLogger - class reference