After reading several articles on how to fine-tuning my code, I found out that the way we are declaring objects and variables could drasticatly impact the performance of our application. This is more and more important as time constraint is now an integral part of some aspects of the Android platform (e.g. result must be provided within 5 sec for some operations).
Now, I found the following code in one of my fragment class (perfectly functional):
Activity myA = getActivity();
if(myA instanceof MainActivity) {
((MainActivity) myA).doNext();
}
Knowing that objects creation/destruction and ressources allocation are some of the factors contributing to drain-out the device’s batteries, I had in mind to rewrite my code to:
if(getActivity() instanceof MainActivity) {
((MainActivity ) getActivity()).doNext();
}
From a functional perspective, both codes are delivering the same results. However, what is the most suitable approach? I’m asking because I see pros and cons to both approach and I’m unable to find the appropriate way to evaluate the performance and I’m also not clear on which counters (memory usage, function' speed, used CPU cycles, etc) the evaluation should be performed.
Thanks for your contribution in advance.