Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT string concatenation operator + vs. stringbuffer

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
like image 749
user431529 Avatar asked Apr 21 '11 18:04

user431529


People also ask

Is StringBuilder better than concatenation?

Conclusion. StringBuilder executes significantly faster than the String class when performing the concatenation or modification operations. Modifying a String creates a new String in the heap memory. To change the content of the String, we should consider the StringBuilder class.

Which is better String or StringBuffer?

The StringBuffer class is used to represent characters that can be modified. The significant performance difference between these two classes is that StringBuffer is faster than String when performing simple concatenations.

What is the difference between String and StringBuffer?

String consumes more as compared to the stringbuffer. StringBuffer uses less memory as compared to the string. It utilises a string constant pool to store the values. It prefers heap memory to store the objects.

What is the major difference between concat () method and operator?

concat() method takes only one argument of string and concatenates it with other string. + operator takes any number of arguments and concatenates all the strings.


1 Answers

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

like image 76
user431529 Avatar answered Oct 07 '22 06:10

user431529