Results 1 to 6 of 6
- 08-22-2011, 11:17 PM #1
String concatenation vs. StringBuilder.
Indeed the StringBuilder class is FAR faster for VERY LONG Strings.
I have created code to time the two below:
For 100000 Strings, the times on my PC are as follows:Java Code:public class StringStuff { public static void main( String[] args ) { StringStuff m = new StringStuff(); m.addStrings(); } public void addStrings() { int i, j; int numberStrings = 100000; long initTime, finalTime; double totalTime; System.out.println( "Using String concatenation" ); String str = "Test"; // set initial time initTime = System.currentTimeMillis(); for( i = 0; i < numberStrings; i++ ) { str += "A"; } // set final time finalTime = System.currentTimeMillis(); totalTime = (double)( finalTime - initTime ) / 1000.0; System.out.printf( "Total time to concatenate %d Strings using String concatenation is %f sec\n", i, totalTime ); System.out.println( "Using StringBuilder class" ); String stp = "Test"; StringBuilder sb = new StringBuilder(); stp = sb.append( stp ).toString(); // set initial time initTime = System.currentTimeMillis(); for( j = 0; j < numberStrings; j++ ) { stp = sb.append( "A" ).toString(); } finalTime = System.currentTimeMillis(); totalTime = (double)( finalTime - initTime ) / 1000.0; System.out.printf( "Total time to concatenate %d Strings using StringBuilder class is %f sec\n", i, totalTime ); } }
String concatenation = 21.5sec
StringBuilder = 5.6sec
With all this being said, I only started to see time differences when I got to 15000 or better. Even at 15K the times were as follows:
String concatenation = 0.48sec
StringBuilder = 0.13secIf you aren't programming in Java, well that's just too bad.
I'd rather be using Ubuntu.
- 08-22-2011, 11:40 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Those timings don't include the extra work required by the JVM. For example, at some point in time all those extra String will need to be garbage collected.
Also, your StringBuffer code test doesn't look fair because you a creating a new String every time. There is no need for the toString() to be invoked during every append() operation. The toString() method should only be invoked after the loop has finished executing.
The general rule is to write readable code and only worry about performance when it is an issue. For my two cents I like the append() method anyway.
- 08-22-2011, 11:57 PM #3
Excellent, I amended the program as follows:
Using String concatenation still took ~21 secs, but the StringBuilder was near instantaneous.Java Code:public class StringStuff { public static void main( String[] args ) { StringStuff m = new StringStuff(); m.addStrings(); System.out.println( args.length ); } public void addStrings() { int i, j; int numberStrings = 100000; long initTime, finalTime; double totalTime; System.out.println( "Using String concatenation" ); String str = "Test"; // set initial time initTime = System.currentTimeMillis(); for( i = 0; i < numberStrings; i++ ) { str += "A"; } // set final time finalTime = System.currentTimeMillis(); totalTime = (double)( finalTime - initTime ) / 1000.0; System.out.printf( "Total time to concatenate %d Strings using String concatenation is %f sec\n", i, totalTime ); System.out.println( "Using StringBuilder class" ); String stp = "Test"; StringBuilder sb = new StringBuilder(); stp = sb.append( stp ).toString(); // set initial time initTime = System.currentTimeMillis(); for( j = 0; j < numberStrings; j++ ) { sb.append( "A" ); } stp = sb.toString(); finalTime = System.currentTimeMillis(); totalTime = (double)( finalTime - initTime ) / 1000.0; System.out.printf( "Total time to concatenate %d Strings using StringBuilder class is %f sec\n", i, totalTime ); } }
Thanks!!!!If you aren't programming in Java, well that's just too bad.
I'd rather be using Ubuntu.
- 08-23-2011, 10:38 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
It's worth bearing in mind that single statement concatenation should be the same as, under the hood, the compiler uses a StringBuilder to do the work.
So the following two should be pretty similar (and the former is easier to read):
(Hopefully that'll compile...)Java Code:String oneLine = "This is my String. " + anotherString + andAnother + " some more text " + lastBit; ... String theOtherLine = new StringBuilder().append("This is my String. ").append(anotherString).append(andAnother).append(" some more text ").append(lastBit).toString();
- 08-23-2011, 11:13 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
It does ;-) Also note that the following variation of your code:
... creates a new StringBuilder for every single String catenation operation.Java Code:String oneLine = "This is my String. "; oneLine+= anotherString; oneLine+= andAnother; oneLine+= " some more text "; oneLine+= lastBit;
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-23-2011, 11:17 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Concatenation with strings
By link6790 in forum New To JavaReplies: 16Last Post: 05-05-2011, 07:33 PM -
Accessing array index through the result of a string concatenation
By colpwd in forum New To JavaReplies: 4Last Post: 08-19-2010, 01:06 AM -
Concatenation file contents
By Java Tip in forum Java TipReplies: 1Last Post: 02-07-2008, 01:29 PM -
Database Column vs String Concatenation
By varun_33 in forum JDBCReplies: 1Last Post: 12-11-2007, 04:14 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks