Valhalla Legends Forums Archive | Java Programming | String vs. StringBuffer

AuthorMessageTime
iago
kp once said this, and I decided to see how much difference there actually is between a String and StringBuffer. My test code is this:
[code] public static void main(String args[])
{
String b = new String();
StringBuffer c = new StringBuffer(50001);

Timer a = new Timer();
for(int i = 0; i < 50000; i++)
{
b = b + "a";
}
System.out.println(a.toString());

b = null;

a.reset();
for(int i = 0; i < 50000; i++)
{
c.append(a);
}
b = c.toString();
System.out.println(a.toString());

}
[/code]

The output is this:
[code]It has been 28373ms (28s) since the timer was created.
It has been 977ms (0s) since the timer was created.[/code]

Bottom line: StringBuffer can make a HUGE difference.
February 17, 2004, 2:47 PM
St0rm.iD
Strings are immutable, thats why.
February 17, 2004, 5:16 PM
iago
[quote author=St0rm.iD link=board=34;threadid=5338;start=0#msg44749 date=1077038168]
Strings are immutable, thats why.
[/quote]

I know why, but I wanted to see how much of a difference it made, just for the sake of curiousity.
February 18, 2004, 6:38 PM
Grok
I don't know Java, but from looking at your code it appears with b you forced it to grow char-by-char, while with c you preallocated its memory. So it would seem natural to expect working with b to take longer. Did I misinterpret?
February 18, 2004, 10:28 PM
Yoni
To add to Grok's comment, do Java Strings have a "reserve" function similar to C++ std::string::reserve, so you can pre-allocate 50001 bytes? I think the comparison would be more interesting this way, since then both benchmarks do the same thing in different ways, instead of benchmark #1 having to do more work as it is now.
February 18, 2004, 11:14 PM
iago
I forgot to mention this, but I also tried this without allocating a StringBuffer of a constant size, like this:
StringBuffer s = new StringBuffer();

And I tried doing this:
t = new Timer();
StringBuffer s = new StringBuffer(50001);

In both cases, it was ~150ms more.

The reason for the speed difference is what Storm said - when you use a String, it's making a new string in every loop rather than resuing the old one. This takes up considerably more resources.

Kp once posted a good example somewhere, but I couldn't find it :(
February 18, 2004, 11:55 PM
Grok
[quote author=iago link=board=34;threadid=5338;start=0#msg44937 date=1077148517]And I tried doing this:
t = new Timer();
StringBuffer s = new StringBuffer(50001);

In both cases, it was ~150ms more.[/quote]

150ms more is a huge difference from

[quote]It has been 28373ms (28s) since the timer was created.
It has been 977ms (0s) since the timer was created.[/quote]
February 19, 2004, 11:22 AM
iago
[quote author=Grok link=board=34;threadid=5338;start=0#msg45016 date=1077189741]
[quote author=iago link=board=34;threadid=5338;start=0#msg44937 date=1077148517]And I tried doing this:
t = new Timer();
StringBuffer s = new StringBuffer(50001);

In both cases, it was ~150ms more.[/quote]

150ms more is a huge difference from

[quote]It has been 28373ms (28s) since the timer was created.
It has been 977ms (0s) since the timer was created.[/quote]

[/quote]

I can't copy/paste, since I'm running this test on a different computer, but here are my results without pre-allocating:
1119ms
1319ms
1077ms

And with pre-allocating within the timer block:
1093ms
1173ms
1088ms

And pre-allocating without the timer block:
1075ms
1088ms
1078ms

In any case, it's still considerably faster to use StringBuffer. The slower results today probably indicate the server is busy with something.
February 19, 2004, 2:16 PM
Tuberload
StringBuffer versus String
What is the performance impact of the StringBuffer and String classes?
http://www.javaworld.com/javaworld/jw-03-2000/jw-0324-javaperf_p.html
February 20, 2004, 8:06 AM

Search