MSerra
MSerra

Reputation: 35

Performance at working with strings android

Could somebody tell me what is better in terms of performance?

Is it better to save 2 strings at string.xml, like 'abc' and 'abc:'

Or should I save only the first one and concatenate ':' when needed at Java coding ???

Upvotes: 3

Views: 217

Answers (3)

IvoTops
IvoTops

Reputation: 3531

Both from storage perspective and performance you should save only "abc";

  • getting extra data from disk takes far longer as some quick in-memory actions.
  • storing the same data twice is bad practice in general

Upvotes: 1

Squonk
Squonk

Reputation: 48871

Very difficult to answer depending on what your strings will represent and what you need to append. Localization is also an issue, for example...

Dog // English
Chien // French
Hund // German

Using string resources allows you to create different resource files depending on the locale of the device and Android will automatically use the right localized string resource file. If all you need to do is append a single character such as : then you'll double every string for every language.

If you choose to only save the basic strings and append the character using code, then the code will be universal and you'll simply need to append the character to whatever localized word - potentially a lot more efficient.

Upvotes: 1

msEmmaMays
msEmmaMays

Reputation: 1063

If you have to concatenate multiple strings you should use StringBuilder - http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuilder.html

It's much faster then using '+' or '.concat()'

Upvotes: 0

Related Questions