Thanks Diane, So the perf optimization documented is just the same as in regular java app, or for that matter, any OO language. I was afraid that for Dalvik VM, the cost of accessing member variable is disproportionally large. Perhaps the HW makes this optimzation more worthwhile.
I understand it's good practice to use local vars whenever possible. However, in a typical android app, I can easily have say ~10 member variables (TextView), and if I use most of them in different methods, it gets a little annoying copying 10 local variable assignments to each method. So I'd rather not track their usage in each method. =) Anyway, just wanted to make sure member variable access is not a major perf issue, which sounds like it's not. Though I agree it's good practice. On Tue, Jan 6, 2009 at 2:44 AM, Dianne Hackborn <[email protected]> wrote: > If you are going to access a variable a few or more times in a method, this > is a very good technique to get in the habit of. I'm not sure I see how it > impacts readability and maintainability that match -- on the contrary, I > think it can help, since you are documenting that the variable will serve as > a constant during the function (as a bonus, declare the local variable as a > final to have the compiler enforce this). > > When accessing a member variable, the compiler will very often need to > dereference 'this' and reload the value from memory each time it is access, > since the value could have changed since it was last used. Accessing a > local variable in contrast can be done just by using the existing value in a > register. > > > On Mon, Jan 5, 2009 at 3:44 PM, Dave Kong <[email protected]> wrote: > >> In the Notepad Exercise 2, there is a section about perf (see excerpt) >> >> *Note:* We assign the mNotesCursor field to a local variable at the start >> of the method. This is done as an optimization of the Android code. >> Accessing a local variable is much more efficient than accessing a field in >> the Dalvik VM, so by doing this we make only one access to the field, and >> five accesses to the local variable, making the routine much more efficient. >> It is recommended that you use this optimization when possible. >> >> Can someone tell me in a bit more precise terms how "much more efficient" >> is to use a local variable? I'm trying to weigh the perf benefit vs. code >> readability/maintainability. It is a pain to reassign n fields to n local >> variables for m methods... >> >> Thanks! >> >> -dave >> >> >> > > > -- > Dianne Hackborn > Android framework engineer > [email protected] > > Note: please don't send private questions to me, as I don't have time to > provide private support. All such questions should be posted on public > forums, where I and others can see and answer them. > > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---

