android - Are dimen values cached? -


is there performance gain on doing following:

final int pixels = getresources.getdimensionpixelsize(r.dimen.pixels); (customview view: views) {     view.setpixelvalue(pixels); } 

vs.

for (customview view: views) {     view.setpixelvalue(getresources.getdimensionpixelsize(r.dimen.pixels)); } 

or dimens values cached / bytecode optimised / other optimisation makes redundant?

looking @ resources.getdimentionpixelsize() , deeper till assetmanager.loadresourcevalue() native it's impossible say. however, resources.getdimentionpixelsize() source it's possible following:

public int getdimensionpixelsize(int id) throws notfoundexception {     synchronized (maccesslock) {         typedvalue value = mtmpvalue;         if (value == null) {             mtmpvalue = value = new typedvalue();         }         getvalue(id, value, true);         if (value.type == typedvalue.type_dimension) {             return typedvalue.complextodimensionpixelsize(                     value.data, mmetrics);         }         throw new notfoundexception(                 "resource id #0x" + integer.tohexstring(id) + " type #0x"                 + integer.tohexstring(value.type) + " not valid");     } } 
  • it lock resources synchronized (maccesslock) (read, if other thread of try access resources same moment - on of them wait);
  • it call float math inside typedvalue.complextodimensionpixelsize();
  • it create typedvalue object;
  • it @ least 5 method calls;

so, maybe it's cached somewhere in native, so, call not free you. here's measurements i've done question (with single thread accessing resources):

    int pixels = 0;     resources res = getresources();     long ms = system.currenttimemillis();     (int = 0; < 100000; i++) {         pixels = res.getdimensionpixelsize(r.dimen.my_dimen);     }      log.e(tag, "time " + (system.currenttimemillis() - ms)); 

on s6 android lollipop gives 816 ms, or 8160 nano-sec / call.

finally, in case (i believe have maximum 10-20 iterations) doesn't matter in terms of performance. in more iterations , / or concurrency in access resources in might significant.

personally suggest outside better style , small still speed improvement.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -