Android : What is the most efficient way to process a multiple value method return?

on Saturday, December 13, 2014


I have a method that returns Point:



private Point getDisplayWH() {

Display display = this.getWindowManager().getDefaultDisplay();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
Point realWH = new Point();
display.getSize(realWH);
return realWH;
}
return new Point(display.getWidth(), display.getHeight());
}


Now, when receiving this result, I can't decide which of these two is more efficient.


Number 1:



Point displayWH = getDisplayWH();
layoutPreviewDim = calcCamPrevDimensions(displayWH.x, displayWH.y));


Number 2:



layoutPreviewDim = calcCamPrevDimensions(getDisplayWH().x, getDisplayWH().y));


In this article it is said that:



If you have a method returning a string, and you know that its result will always be appended to a StringBuffer anyway, change your signature and implementation so that the function does the append directly, instead of creating a short-lived temporary object.



But should I follow these instructions even if I have to call the method more than once to get the return? What about display variable in my getDisplayWH() method? Is it more efficient approach than accessing Display methods directly this.getWindowManager().getDefaultDisplay().xxxx?


And my second side question: Is there a general way to check/compare code efficiency other than with system time difference before and after method call? Or the time difference is the ultimate tool?


0 comments:

Post a Comment