java - Android Performance - 'Avoid Internal Getters/Setters' -
java - Android Performance - 'Avoid Internal Getters/Setters' -
just read on dev site:
avoid internal getters/setters
in native languages c++ it's mutual practice utilize getters (e.g. = getcount()) instead of accessing field straight (i = mcount). first-class habit c++, because compiler can inline access, , if need restrict or debug field access can add together code @ time.
on android, bad idea. virtual method calls expensive, much more instance field lookups. it's reasonable follow mutual object-oriented programming practices , have getters , setters in public interface, within class should access fields directly.
without jit, direct field access 3x faster invoking trivial getter. jit (where direct field access inexpensive accessing local), direct field access 7x faster invoking trivial getter. true in froyo, improve in future when jit inlines getter methods.
so saying utilize field access within class:
public class myobject { public object innerobject; // private if using getter public void dosomestuff(){ if(innerobject){ // within class access // .... } } public object getinnerobject(){ // removed if using field access homecoming innerobject; } } but access object?:
public class secondobject { public void dosecondsomething(){ myobject ob = new myobject(); object inner; //this question (from android performance perspective) inner = ob.getinnerobject(); // or inner = b.innerobject } }
the performance nail of using internal getters , setters applies external getters , setters.
however, in external case getters , setters have important benefits in other areas; e.g. preserving encapsulation, reducing harmful coupling, making code more maintainable, , on. so, regarded best practice utilize getters , setters despite performance nail may incur.
the performance nail result of limitations of the current generation of older android jit compilers. situation is sure improve has improved gingerbread. (reference - http://stackoverflow.com/a/4930538/139985 ... , note wrote answer!)
in general, bad thought "tune" code inferior platform, if there reasonable chance improve 1 in offing.
java android performance optimization
Comments
Post a Comment