1

I have always tried to use the resource in Android to declare my strings in Android, ever since I had a translation issue. But there are times I don't need to declare strings, like the strings I use for log messages. But I did a little search and found that nobody has tested what is faster, loading a string from strings.xml in code or hard coding it in line. Here is the normal way I use log messages:

log.d(TAG, "This is My Hard Coded String");

Compared to putting "This is My Hard Coded String" in strings.xml and then calling it in code:

log.d(TAG, getResurce().getString(R.string.hardcodedstring));

I've tried running some test times these but not sure I have considered all aspects. So what is faster has anyone else done this?

4 Answers 4

2

The Test Results I got were this for Inline code the average time of writing out 100 times to log files was:

20.6 ms

The average time for getResource string call and writing to log file 100 times was:

29.0 ms

I was surprised I thought the getResources() call would be faster.

Sign up to request clarification or add additional context in comments.

Comments

1

If your application bottleneck is getString(), you've done a pretty damned amazing job of optimizing everything else... network, db-access, painting your views.

I really wouldn't worry about it.

3 Comments

Again this is more a question of curiosity on what really is faster.
I'd theorize that using getString() is going to be faster, as the strings.xml is compiled and will translate to a bunch of pointers of resources. Where as using strings in your Log.d is going to create the strings, use more memory etc. Try it out?
I theorized too but I want to see if someone else has actually tested this and come up with results. I am trying to confirm my tests.
1

Offcourse hard coding the string will be faster but it is against Java coding standard because to store in constant file or in properties or in xml file then you can reuse it.

1 Comment

Its a log comment so reuse is not a matter.
0

I don't think either is going to make your application much faster, using multiple strings of the same value (Strings.xml or programatically) doesn't have any associated overhead. All strings and string-valued expressions are reused rather than re-created if you use it again.

The point is just to easily access and edit every occurence of each term, and also supporting multiple languages.

3 Comments

You theorized about it but did you actually test it?
It is Oracle itself who claims this theory.. "All literal strings and string-valued constant expressions are interned."
But I am trying to find out what is the actual results. Oracle may claim but it may not be faster. There are a ton things that are in-efficient in Java that I have re-written. Anyway this may not be a pure Java thing it may be an Android resource loader issue too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.