JPM
JPM

Reputation: 9296

What is Faster Using Hard Coded InLine String or String in Strings.xml

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?

Upvotes: 1

Views: 974

Answers (4)

JPM
JPM

Reputation: 9296

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.

Upvotes: 2

Qazi
Qazi

Reputation: 132

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.

Upvotes: 1

jlindenbaum
jlindenbaum

Reputation: 1881

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.

Upvotes: 1

Schaemelhout
Schaemelhout

Reputation: 705

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.

Upvotes: 0

Related Questions