On 26/12/10 18:04, Soumya wrote:
TextView tv = (TextView) findViewById(R.id.sampleTextView);
tv.setText("Hello World");

-- OR --

((TextView)  findViewById(R.id.sampleTextView)).setText("Hello
World");


Upon re-reading the original email, I retract my previous reply. Even if the optimizer for Java sucks royally, there is no significant difference between the JVM produced for both codes, and by the time its dalvik I can't see how there is any difference at all.

First, the garbage collection claim. As "findViewById" returns a reference to an object that has been created long before this code gets to run, no extra instances are created by this code, and so nothing needs to be garbage collected. The only difference between the two variants is the existence of a named reference. These references are not the responsibility of the garbage collector to begin with, so we are left with the simpler question of whether we are wasting a reference here.

I still stand by my original statement, that the optimizer should be smart enough to realize that the variable is never used after a certain point in the code, and allocate that space to other needs. Even if that is not the case for the JVM code, however, the dalvik post-processor needs to translate the stack based allocation to register based one. When that happens, it should pick up on this change (if not picked up earlier).

Either way, its fairly easy to check. Just compile both variants and run baksmali on the result. Check whether later code reuses the register that findViewById dumps its result into. If it does, there is no run time difference between the two.

Shachar

--
Shachar Shemesh
Lingnu Open Source Consulting Ltd.
http://www.lingnu.com

--
unsubscribe: [email protected]
website: http://groups.google.com/group/android-porting

Reply via email to