Tom McFarlin
Tom McFarlin

Reputation: 829

Use "findViewById" repetitively or store View?

Being the efficiency freak that I am (as I'm sure lots of you all are as well), I've wondered this for a while and just thought of posing this question:

Two scenarios, possibly two different answers.

If I'm looping through a list of items and updating a bunch of EditTexts in a LinearLayout as such, what are the pros/cons of these two methods: 1)

for (int i = 0; i < itemList.size(); i++) {
    ((TextView)findViewById(itemList.get(i).getId())).setText(itemList.getText());
    ((TextView)findViewById(itemList.get(i).getId())).setColor(itemList.getColor());
}

2)

TextView tv;
for (int i = 0; i < itemList.size(); i++) {
    tv = ((TextView)findViewById(itemList.get(i).getId()));
    tv.setText(itemList.getText());
    tv.setColor(itemList.getColor());
}

I think the underlying question is how efficient is "findViewById"? This may be picky, I think 2) is the better solution. Thanks!

Upvotes: 1

Views: 984

Answers (7)

Abdullah Alhomidi
Abdullah Alhomidi

Reputation: 21

The second is for sure less expensive by 50%. But I would prefer @TimCastelijns 3rd method because he is dumping the view reference at the end of the loop.

In the first method, you use findViewById twice. In the second method, you use it once and save a reference to it, which saves 50% off of the resource usage.

I preferred @TimCastelijns, because he saves it as a local variable, which will be dumped, therefore saving resources.

Upvotes: 0

ChatterOne
ChatterOne

Reputation: 3541

With your second option you save: - A call to findViewById() - A call to itemList.get(i) - A call to [itemList.get(i)] getId()

Also, note that in a for loop usually going backward is a little bit faster (more optimized) than going forward (because i < value translates to i-value < 0, which is more expensive than i > 0).

Upvotes: 2

Rami
Rami

Reputation: 7929

Second way is better, because the cost of findViewById() is acceptable in static UI layouts. However, since getView() is called frequently, the usage of findViewById() should be kept to minimum.

Upvotes: 0

user468311
user468311

Reputation:

You should use new RecyclerView if possible now. Combined with LinearLayoutManager it'll allow you achieve the same, but you'll be forced to use ViewHolder pattern. If you go with your ListView, you should also implement ViewHolder. findViewById is definitely not efficient, so you need to prevent too many calls to it.

Upvotes: 0

Simas
Simas

Reputation: 44188

A google employee Dianne Hackborn has answered a very similar question here.

She says that you should avoid using findViewByid repetitevely whenever you can.

Upvotes: 1

Tim
Tim

Reputation: 43394

This is not picky at all. 2nd option is without doubt the better one.

1

for (int i = 0; i < itemList.size(); i++) {
    ((TextView)findViewById(itemList.get(i).getId())).setText(itemList.getText());
    ((TextView)findViewById(itemList.get(i).getId())).setColor(itemList.getColor());
}

Looks clean, but isn't. If you are working with one and the same textview, absolutely do not call findViewById more than once.

2

TextView tv;
for (int i = 0; i < itemList.size(); i++) {
    tv = ((TextView)findViewById(itemList.get(i).getId()));
    tv.setText(itemList.getText());
    tv.setColor(itemList.getColor());
}

This is the better option, because it only calls findViewById once. It's a little less readable, though.

You could also consider a 3rd option

for (int i = 0; i < itemList.size(); i++) {
    TextView tv = ((TextView)findViewById(itemList.get(i).getId()));
    tv.setText(itemList.getText());
    tv.setColor(itemList.getColor());
}

This keeps everything in the loop (easier to read, imo) without notably sacrificing efficiency. I prefer the 3rd, but the 2nd is a good pick as well.

Upvotes: 1

Thomas Kaliakos
Thomas Kaliakos

Reputation: 3304

I think without doubt the second option is better.
Not only you save the cost of calling findViewById one extra time (ok at the cost of one extra local variable)
but the code is also much more readable.

Upvotes: 0

Related Questions