Performance++ View.findViewById() vs Activity.findViewById()

Tutorials with advanced 'difficulty' and more Lines of Code.

Performance++ View.findViewById() vs Activity.findViewById()

Postby plusminus » Sat Sep 06, 2008 6:07 am

Performance++ View.findViewById() vs Activity.findViewById()


This is how you can speed up the loadingspeed of your application, as Activity's [font=Courier New]findViewByID[/font], like you probably used it up to now, is not that fast as you think. As you know the Android Layout-Structure is hierarchical (just like HTML and XML) and therefore the easiest way to represent it is a tree. The effect is bigger, as your layout gets more complex :!:

Simpliefied Example Layout for this tutorial:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <LinearLayout>
  2.      <LinearLayout id="@+id/layout_outer">
  3.            <TextView id="@+id/tv_inner_1"/>
  4.            <TextView id="@+id/tv_inner_2"/>
  5.      </LinearLayout>
  6. </LinearLayout>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


So if you do the following :darrow: the Android-Runtime has to traverse the whole layout-tree every time you call findViewById (of your Activity). The better way is to call myView.findViewById when you habe nested a nested layout.

So, getting the both TextViews of the both TextViews from the example before worked like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. TextView tv_inner_1 = (TextView)this.findViewById(R.id.tv_inner_1);
  2. TextView tv_inner_2 = (TextView)this.findViewById(R.id.tv_inner_2);
Parsed in 0.029 seconds, using GeSHi 1.0.8.4

Where the following is the faster way:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. View layout_outer = this.findViewById(R.id.layout_outer);
  2. TextView tv_inner_1 = (TextView)layout_outer.findViewById(R.id.tv_inner_1);
  3. TextView tv_inner_2 = (TextView)layout_outer.findViewById(R.id.tv_inner_2);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

Thats it :)


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby a.fisher » Tue Nov 03, 2009 11:46 am

Doing it this way gives you another findViewById and instantiates a field for layot_outer. Does this impact optimization at all?
a.fisher
Freshman
Freshman
 
Posts: 5
Joined: Wed Oct 14, 2009 10:23 am

Postby divestoclimb » Thu Nov 19, 2009 7:14 pm

I only timed my app once using each method, but there's definitely some merit to this. My activity has three tabs and lots of views, so I expected some improvement.

Calling only Activity.findViewById: activity launches in ~4000ms according to adb logcat
Calling findViewById methods of parent views: activity launches in ~2400ms

Woohoo!
divestoclimb - Android SCUBA app developer
Android Community | Gas Mixer, ScubaLib, Scuba Tanks | d2c Checklist
All code is open source under Apache license.
divestoclimb
Developer
Developer
 
Posts: 33
Joined: Mon May 11, 2009 7:46 pm

little help

Postby brccabral » Mon Jan 25, 2010 1:30 am

could you look at this problem?
viewtopic.php?p=34051
thanks
Att,
Bruno Cabral
brccabral
Freshman
Freshman
 
Posts: 3
Joined: Sun Jan 24, 2010 11:55 pm

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests