3

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 ???

3 Answers 3

1

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.

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

1 Comment

Right, that's what i thought. If it would be to append two strings, both that could be used for different regions/languages, yes it would make sense to store both. But in this case all i need is to append single universal chars, so i'll store just the string. Thank you!!!
1

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

1 Comment

Good answer too. Yes, it's a bit like everything else. Thank you!
0

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()'

2 Comments

This is a myth. The compiler turns string concatenation using + into the same byte codes that you would end up with by using StringBuilder. The only time you want to bother with StringBuilder is when you are building up a string over several statements. As to "much faster" -- do you have benchmarks to back this up?
He's right, I double checked - After you said that I realized I originally read that for Visual Basic 6 (in which case it was faster) - kaioa.com/node/59

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.