开发者

Measure time for rendering ListView

How can I measure time that ListView/ListActivity takes for rendering its list?

I have a ListActivity that takes a long time to show (on older devices it's ~3s) and I'd like to optimize that.

EDIT:

I already have time between Activity1 and Activity2. During this time, Activity2 is doing some stuff (initializing). Among other things, it renders its list. I want to get time this activity takes to render that list. If total time between activities is 3s, I want to know whether rendering list takes 2.9s or 0.5开发者_JAVA技巧s....


You could simply ouput the time. For example you could use the logcat

final long t0 = System.currentTimeMillis();

// code to measure

Log.w(TAG, "TEXT" + System.currentTimeMillis()-t0);

Of course you could use any other system for the ouput like a dialog or stuff. Just use what you like.

EDIT:

If you don't want to use a debug message in your code all the time you could do it like this:

Create a class called settings:

public class Settings {
public static final boolean DEBUG = true;
// If you prefer you could do use an enum
// enum debugLevel {SHOW_EVERYMESSAGE, ERRORS, IMPORTANT_MESSAGES, ...}
// In your classes you would have to check that DEBUG is less or equal than
// the debugLevel you want 
}

In classes where you want to use a debug message simply do this

import xxx.yyy.Settings

class foo {
  final static boolean DEBUG = Settings.DEBUG;
  if(DEBUG){
    // Debug messages
  }
}

Now if you want to disable DEBUG messages you could simply set DEBUG = false in your Settings class.

If you want to measure between two activities you could use intents and send t0 with an intent to the other activity to compute the time. Of course you could include this with if(DEBUG){ /* code */ } statements to spare the sending of the intent in the final release. The if statements should not increase the computation of your code too dramatically.


I cannot tell if Java offers a better implementation using System.currentTimeMillis() or System.nanoTime(). Nevertheless, you should give the TimingLogger class a try. Take a look at this article describing the usage of the TimingLogger helper class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜