7

I had to choose a way of efficient string string concatenation for GWT application. For this I did a small test and thought it will be helpful for others to know results as well.

So, surprisingly difference is quite minor: ~100ms for 1000000 concatenations. So, please choose appropriate from code reading point of view.

My testing was simple:

// + operator
private void str() {
    Date start = new Date();

    String out = "";
    for(int a=0;a<1000000;a++) {
        out += "item" + a;
    }

    Date end = new Date();

    MessageBar.error("str:" + (end.getTime() - start.getTime()));
}

// StringBuffer implementation
private void sb() {
    Date start = new Date();

    StringBuffer out = new StringBuffer();
    for(int a=0;a<1000000;a++) {
        out.append("item" + a);
    }

    Date end = new Date();

    MessageBar.error("sb:" + (end.getTime() - start.getTime()));
}

Results were:

str:1612
str:1788
str:1579
sb:1765
sb:1818
sb:1839
3
  • 1
    This is not a question :) In general, concatenation is more expensive because String is immutable. Java has to create a new String object each time. Commented Apr 21, 2011 at 18:09
  • 2
    Did you check this across different browsers? And the difference between hosted and deployed versions? Commented Apr 21, 2011 at 18:16
  • 1
    Please phrase your post as a question and put the answer below. Commented Apr 21, 2011 at 20:31

2 Answers 2

8

Following question of stan229 and request of Bill the Lizard.

That's indeed interesting how performance differs from browser to browser. For me the question was "which concatenation method to choose" and I got the answer I wanted. But here is more test results:

chrome 10.0.648.204:
str: 748
sb : 849

firefox 3.6.16:
str: 1681
sb : 1861

ie 8:
str: 2484
sb : 4094

opera 11.10
str: 802
sb : 792

So, the answer I got is: + operator gives better performance

My next question is what gives better performance:

int i=0;
// this
String result = String.valueOf(i);
// or this
String result = i + "";

will post this once I do the test or, if you have the answer - please post

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

1 Comment

The differences for the browsers you tested are small - and due to a little bit of overhead, StringBuffer could even have a little disadvantage. But for older IE browsers, the difference is enormous, in favor of StringBuffer. My results on IE 6: "str: 90266 sb: 1203" (note that I reduced the loop count to 50000, because 1000000 would have taken quite long on my netbook.) Not sure about IE 7, but I think the situation is similar there.
0

You can look at the gwt source code and see how StringBuffer/StringBuilder are emulated. GWT chooses the best perfomant way for string concatenation for the browsers.

GWT 2.2.0 StringBuffer source

A fast way to create strings using multiple appends. This is implemented using a StringBufferImpl that is chosen with deferred binding. Most methods will give expected performance results...

1 Comment

I see what you mean, but my tests show better performance when using normal + concatenation. So, I myself will use it with no doubts, like I had before. In shared code, which is used on both client and server I will use StringBuffer, of course. Often I need to generate HTML code and there + is better (both readability & performance).

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.