• Welcome to Valhalla Legends Archive.
 

String vs. StringBuffer

Started by iago, February 17, 2004, 08:47 AM

Previous topic - Next topic

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:
       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());

       }


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


Bottom line: StringBuffer can make a HUGE difference.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Banana fanna fo fanna

Strings are immutable, thats why.

iago

Quote from: St0rm.iD on February 17, 2004, 11:16 AM
Strings are immutable, thats why.

I know why, but I wanted to see how much of a difference it made, just for the sake of curiousity.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


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?

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.

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 :(
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Grok

Quote from: iago on February 18, 2004, 05:55 PMAnd I tried doing this:
t = new Timer();
StringBuffer s = new StringBuffer(50001);

In both cases, it was ~150ms more.

150ms more is a huge difference from

QuoteIt has been 28373ms (28s) since the timer was created.
It has been 977ms (0s) since the timer was created.

iago

Quote from: Grok on February 19, 2004, 05:22 AM
Quote from: iago on February 18, 2004, 05:55 PMAnd I tried doing this:
t = new Timer();
StringBuffer s = new StringBuffer(50001);

In both cases, it was ~150ms more.

150ms more is a huge difference from

QuoteIt has been 28373ms (28s) since the timer was created.
It has been 977ms (0s) since the timer was created.


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.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


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
Quote"Pray not for lighter burdens, but for stronger backs." -- Teddy Roosevelt
"Your forefathers have given you freedom, so good luck, see you around, hope you make it" -- Unknown