-2

Possible Duplicate:
StringBuilder vs String concatenation in toString() in Java

In my code I am using String concatenation like below. I am doing code review so i need to do performance check. Please any one suggest that which one is better to use.

quoteTreeMap.put(index + CommonUtil.QUOTE_TREE_DOT + fundIndex + CommonUtil.QUOTE_TREE_DOT + lastIndex,buffer.toString());

so we are using this repeatedly with

index + CommonUtil.QUOTE_TREE_DOT + fundIndex + CommonUtil.QUOTE_TREE_DOT + lastIndex

only lastIndex this value will change, remaining is same for all.

How to write the code to give better performance? can i declare

index + CommonUtil.QUOTE_TREE_DOT + fundIndex + CommonUtil.QUOTE_TREE_DOT

this as top like

String one=""index + CommonUtil.QUOTE_TREE_DOT + fundIndex + CommonUtil.QUOTE_TREE_DOT"

and can i use it as one+lastIndex?
for ref:

StringBuffer buffer = new StringBuffer();
buffer.append(CommonUtil.QUOTE_TREE_GROSS_PREM);
buffer.append(CommonUtil.QUOTE_TREE_SPACE);
buffer.append(CommonUtil.QUOTE_TREE_EQUALS);
buffer.append(CommonUtil.QUOTE_TREE_SPACE);
buffer.append(chcCalBreakDownObj.getPremium().getGrossPrem());
quoteTreeMap.put(index + CommonUtil.QUOTE_TREE_DOT + fundIndex + CommonUtil.QUOTE_TREE_DOT + lastIndex, buffer.toString());
lastIndex++;
clearBuffer(buffer);

I am doing code review so i need to do performance check. Please any one suggest that which one is better to use.

Community
  • 1
  • 1
suresh
  • 3
  • 1
  • 8
  • 1
    "I am doing code review so i need to do performance check." So what's stopping you? – NPE Dec 21 '12 at 10:15
  • 2
    Perhaps the worst-formatted question I've ever seen. Please read the editing help to avoid such catastrophes in the future. – Duncan Jones Dec 21 '12 at 10:20
  • " I am doing code review so i need to do performance check." I really don't get how the two are related. What is it? A code review or a performance review? – Gimby Dec 21 '12 at 10:26
  • http://stackoverflow.com/questions/1532461/stringbuilder-vs-string-concatenation-in-tostring-in-java – RobEarl Dec 21 '12 at 10:26

6 Answers6

7

You should use the StringBuilder because it does not involve creating a new String each time you append something to it.

Using the + operator to concatenate two Strings create a new String object.

Please keep in mind that the main difference between StringBuilder and StringBuffer is that the latter is synchronized thus generally slower than the former.

Check out the official documentation if you are interested.

Adam Arold
  • 29,285
  • 22
  • 112
  • 207
  • 3
    I don't think this is good advice. Of the three main alternatives (`String`, `StringBuilder` and `StringBuffer`), the latter is probably the least appropriate. Also, the second paragraph is generally not true. – NPE Dec 21 '12 at 10:17
  • 4
    I'm pretty sure Java uses a StringBuilder or StringBuffer internally to do the concatenation anyway, doesn't it? – cHao Dec 21 '12 at 10:18
  • @cHao.. Yes, it does uses StringBuffer, AFAIK. But, it also creates a new String object for each concatenation it does. – Rohit Jain Dec 21 '12 at 10:20
  • @RohitJain It uses **StringBuilder** not StringBuffer internally for string concatenation – vishal_aim Dec 21 '12 at 10:23
  • I think that using the Builder/Buffer classes are more versatile thus recommended. – Adam Arold Dec 21 '12 at 10:23
  • @vishal_aim.. Ok. Wasn't exactly sure about that. – Rohit Jain Dec 21 '12 at 10:24
  • I don't get it guys. It may involve using `StringBuilder` internally but if you reference the concatenated string like in the example (`String one =`) and append something to it you end up with 2 new `String` objects. – Adam Arold Dec 21 '12 at 10:29
  • 1
    @Adam: But he *doesn't* do that. So there goes your main argument for using a buffer. He's creating a prefix string, which can conceivably be used over and over. It'd actually be the same amount of char shuffling (or even less!) to say `one + lastIndex` than to stuff everything into a new buffer each time. – cHao Dec 21 '12 at 10:30
  • 1
    And in the undefined future he **will** concatenate something to it. It always happens. The statement becomes long etc...I have seen it many times. – Adam Arold Dec 21 '12 at 10:36
  • String one=""index + CommonUtil.QUOTE_TREE_DOT + fundIndex + CommonUtil.QUOTE_TREE – suresh Dec 21 '12 at 10:36
  • quoteTreeMap.put(one+lastIndex); – suresh Dec 21 '12 at 10:36
  • You just created 2 new `String` objects. The first being `one` the second is in the `put` method. – Adam Arold Dec 21 '12 at 10:37
  • 1
    @sureshsudineedi: Sure you *can* do it. It's legal Java. The question is whether it's a good idea. If you're going to do this just with Strings, you should either pre-create `one` (outside of any loops, preferably even outside of the function if possible) or do all the concatenation (including `lastIndex`) in one line. Otherwise, a StringBuilder would probably be better. – cHao Dec 21 '12 at 10:38
  • or buffer.append(index); buffer.append(CommonUtil.QUOTE_TREE_DOT); – suresh Dec 21 '12 at 10:39
  • quoteTreeMap.put(buffer.toString()+lastIndex); can i do this? – suresh Dec 21 '12 at 10:39
  • 1
    @sureshsudineedi: `buffer.toString() + lastIndex` gets you the worst of both worlds. (You get the ugliness of buffers, plus the inefficiency of creating extra strings.) Don't do that. – cHao Dec 21 '12 at 10:40
  • ok. finally give the conclusion how to use it. only "latIndex" value will change remaining String is always same – suresh Dec 21 '12 at 10:43
  • finally i need to add it as a key vale to map – suresh Dec 21 '12 at 10:43
  • @sureshsudineedi: In that case, create `one` elsewhere (outside of the hot spots), and say `one + lastIndex`. – cHao Dec 21 '12 at 10:44
  • actually we need to add 1.1.2.1 as key vale to map. so last 1 value will change. remaining will be same – suresh Dec 21 '12 at 10:44
3

To me it sounds like you're optimizing prematurely.

Focus on writing code that is easy to understand and maintain. Then profile it to discover where the bottlenecks are, and only then optimize.

The difference in performance between the two versions that you post is likely to be irrelevant.

P.S. In any event, it is worth understanding the differences between StringBuffer and StringBuilder. See Difference between StringBuffer and StringBuilder class

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

you can use StringBuilder or StringBuffer. StringBuilder is threadsafe for multiple threads and StringBuffer is usefull for single thread t increases the execution speed for single thread.

Adam Arold
  • 29,285
  • 22
  • 112
  • 207
1

You can use either StringBuffer or StringBuilder. The difference is that StringBuffer is threadsafe. If you use this only inside a method, you'll chose StringBuilder.

Ivan Frolov
  • 1,008
  • 8
  • 6
1

you should profile the code first and find the real performance hogs. using StringBuilder in the part of the code shown might be quicker - but how much do you gain overall?

opi
  • 244
  • 2
  • 9
1

If the resulting one will be the same for every iteration, pre-generate it outside of your loop. Then you can say one + lastIndex without worrying so much about inefficiency; it'd be at least as efficient (CPUwise) as using a buffer explicitly. Probably more so, as you're not rebuilding the string from scratch every time. It does create one semi-extra String, but it's either that or a StringBuilder, so.

If index, fundIndex, or CommonUtil.QUOTE_TREE_DOT will be changing constantly, you have two choices:

  • Use a StringBuilder to generate the result over several lines, or
  • Do all the concatenation in one line. ie: No String one = ....

Just about any other solution will result in extra Strings.

cHao
  • 84,970
  • 20
  • 145
  • 172