Results 1 to 6 of 6
- 01-12-2012, 02:09 AM #1
String vs. StringBuilder vs. StringBuffer
Hello all. I recently was doing a project that did MASSIVE amounts of String concatenation. After much reading and trial and error it is painfully obvious that the String class is not the way to go.
The String.concat( String ) is indeed faster than the += operator. When in doubt, use either the StringBuffer(thread safe) or StringBuilder(not thread safe but faster than StringBuffer)
Example code:
This is the output I get:Java Code:public class StringTimer { public static void main( String[] args ) { StringTimer m = new StringTimer(); m.stringTimer(); } public void stringTimer() { double startTime = 0, endTime = 0, totalTime = 0; int i = 0; int numStrings = 70000; // String String str = new String(); startTime = (double)System.currentTimeMillis(); for( i = 0; i < numStrings; i++ ){ //str+="Hello"; // worse than String.concat( String ) str = str.concat( "Hello" ); } endTime = (double)System.currentTimeMillis(); totalTime = ( endTime - startTime ) / 1000.0; System.out.println( totalTime + " secs to concatenate " + numStrings + " Strings together using String.concat( String )." ); // StringBuilder StringBuilder sb = new StringBuilder(); startTime = (double)System.currentTimeMillis(); for( i = 0; i < numStrings; i++ ){ sb = sb.append( "Hello" ); } endTime = (double)System.currentTimeMillis(); totalTime = ( endTime - startTime ) / 1000.0; System.out.println( totalTime + " secs to concatenate " + numStrings + " Strings together using StringBuilder.append( String )." ); // StringBuffer StringBuffer sbuf = new StringBuffer(); startTime = (double)System.currentTimeMillis(); for( i = 0; i < numStrings; i++ ){ sbuf = sbuf.append( "Hello" ); } endTime = (double)System.currentTimeMillis(); totalTime = ( endTime - startTime ) / 1000.0; System.out.println( totalTime + " secs to concatenate " + numStrings + " Strings together using StringBuffer.append( String )." ); } }
Java Code:>javac StringTimer.java >java StringTimer 15.75 secs to concatenate 70000 Strings together using String.concat( String ). 0.0 secs to concatenate 70000 Strings together using StringBuilder.append( String ). 0.0 secs to concatenate 70000 Strings together using StringBuffer.append( String ).
If you aren't programming in Java, well that's just too bad.
I'd rather be using Ubuntu.
- 01-12-2012, 02:30 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: String vs. StringBuilder vs. StringBuffer
What do the times for += look like?
I'm no expert about timing programs like this (all I know is that they can be trickier than they look) but often you see the "experiment" repeated multiple times within a loop because the first time around can have significantly different times. Hard to argue with anything vs 0 though! Perhaps you could report the times in ms or increase 70000.
- 01-12-2012, 02:32 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: String vs. StringBuilder vs. StringBuffer
Also a little point about StringBuilder being faster than StringBuffer: I half remember seeing somewhere that the hotspot compiler is clever enough to figure out if the thread safe version is unnecessary and use the faster one at runtime.
- 01-12-2012, 02:47 AM #4
Re: String vs. StringBuilder vs. StringBuffer
Here are the values in ms.
Java Code:15812.0 ms to concatenate 70000 Strings together using String.concat( String ). 0.0 ms to concatenate 70000 Strings together using StringBuilder.append( String ). 0.0 ms to concatenate 70000 Strings together using StringBuffer.append( String ).
If you aren't programming in Java, well that's just too bad.
I'd rather be using Ubuntu.
- 01-12-2012, 04:26 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: String vs. StringBuilder vs. StringBuffer
Thanks. I just had a look and it seems you need about 1/2 million strings before you can see StringBuilder/Buffer taking any time at all.
- 01-12-2012, 07:52 AM #6
Similar Threads
-
String concatenation vs. StringBuilder.
By stchman in forum New To JavaReplies: 5Last Post: 08-23-2011, 11:17 AM -
Easiest way to convert String to StringBuffer
By unideal in forum New To JavaReplies: 4Last Post: 12-13-2009, 12:43 PM -
java.lang.StringBuffer.append(StringBuffer.java(Co mpiled Code))
By Ashok Dave in forum Advanced JavaReplies: 3Last Post: 03-04-2009, 06:03 AM -
Difference between StringBuilder & StringBuffer
By Pooja Deshpande in forum New To JavaReplies: 5Last Post: 04-16-2008, 12:51 PM -
StringBuilder v/s StringBuffer
By Pooja Deshpande in forum New To JavaReplies: 9Last Post: 04-11-2008, 09:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks