Results 1 to 6 of 6
  1. #1
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default 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:

    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 )." );
        }
    }
    This is the output I get:
    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.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  4. #4
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default Re: String vs. StringBuilder vs. StringBuffer

    Quote Originally Posted by pbrockway2 View Post
    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.
    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.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  6. #6
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default Re: String vs. StringBuilder vs. StringBuffer

    Quote Originally Posted by pbrockway2 View Post
    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.
    The += operator took about 3X longer than the String.concat() method.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

Similar Threads

  1. String concatenation vs. StringBuilder.
    By stchman in forum New To Java
    Replies: 5
    Last Post: 08-23-2011, 11:17 AM
  2. Easiest way to convert String to StringBuffer
    By unideal in forum New To Java
    Replies: 4
    Last Post: 12-13-2009, 12:43 PM
  3. Replies: 3
    Last Post: 03-04-2009, 06:03 AM
  4. Difference between StringBuilder & StringBuffer
    By Pooja Deshpande in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 12:51 PM
  5. StringBuilder v/s StringBuffer
    By Pooja Deshpande in forum New To Java
    Replies: 9
    Last Post: 04-11-2008, 09:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •