I'm trying to figure out the difference between accessing android string resources. The following quote is not clear to me:
Access by referene is fast
Direct access is slow
access by reference means: setTitle(R.string.title)
direct access means: setTitle(getResources().getString(R.string.title))
Now I've run some speed tests on the android emulator:
access by reference:
for(int i = 0; i< 100000; i++) {
setTitle(R.string.app_name);
}
This took 5090 milliseconds. In contrast I run the same code, using direct access:
for(int i = 0; i< 100000; i++) {
setTitle(getResources().getString(R.string.app_name));
}
This took 5191 milliseconds. I tested this with Android 4.2.2.
So for me it looks a lot like it doesn't matter which way I use the resources. Did this matter in earlier android versions? Does this matter on real devices? In other words: Does it matter which access I choose?
If more parameters of my testing are needed, I'm happy to give them. Thank you for taking the time, appreciate it a lot.
Just look at the code :
(in Activity)
public void setTitle(int titleId) {
setTitle(getText(titleId));
}
(in Context)
public final CharSequence getText(int resId) {
return getResources().getText(resId);
}
So basically, it is exactly the same thing.
What is much slower, however, is if you use Resource.getIdentifier(String, String, String)
to find the ids of your resources.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With