8 Replies Latest reply: Nov 15, 2007 10:07 AM by 796254 RSS

    Performace of StringBuilder - StringBuffer - which one is better

    807603
      Javadoc mention that StringBuilder performance is better then that of a StringBuffer and whenever the class is used by a single thread , StringBuilder is faster . I tried creating a long StringBuilder and a long StringBuffer and compared the performace of both. If size >10000 , then the append method performs faster for StringBuffer rather then StringBuilder.

      Anyone has any idea while StringBuffer should be faster in this case ?
      Thanks .
        • 1. Re: Performace of StringBuilder - StringBuffer - which one is better
          800322
          To be brutally honest: I think it's because your tests are faulty. Running tests like those isn't trivial. Please show us your test code.
          • 2. Re: Performace of StringBuilder - StringBuffer - which one is better
            791266
            wachikiwaka wrote:
            Javadoc mention that StringBuilder performance is better then that of a StringBuffer and whenever the class is used by a single thread , StringBuilder is faster . I tried creating a long StringBuilder and a long StringBuffer and compared the performace of both. If size >10000 , then the append method performs faster for StringBuffer rather then StringBuilder.
            10000 is very small value, how were you able to time it?
            • 3. Re: Performace of StringBuilder - StringBuffer - which one is better
              807603
              Well , The tests I am running are indeed trivial . I will share them .

              int num=10000;
                        StringBuffer sb = new StringBuffer("test");
                        long t1=System.currentTimeMillis();
                        for (int i=0;i<num;i++){
                             sb.append("x");
                        }
                        System.out.println(sb);
                        long buffertime = System.currentTimeMillis() -t1;
                        
                        StringBuilder sb1 = new StringBuilder("test");
                        long t2=System.currentTimeMillis();
                        for (int i=0;i<num;i++){
                             sb1.append("x");
                        }
                        System.out.println(sb1);
                        long buildertime = System.currentTimeMillis() -t2;
                        if(buildertime>buffertime)
                             System.out.println("Buffer is faster");
                        else
                             System.out.println("Builder is faster");


              Output is
              n=50
              Builder is faster

              n=500 (This result is consistent)
              testxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
              Builder is faster

              The result for n=500 is consistent and always shows that StringBuffer is faster .

              Can you suggest some non-trivial tests which can verify that StringBuilder is indeed faster ?
              • 4. Re: Performace of StringBuilder - StringBuffer - which one is better
                807603
                For num=1000000;

                again StringBuffer is faster .
                • 5. Re: Performace of StringBuilder - StringBuffer - which one is better
                  800322
                  What happens if you switch the execution order of those tests?
                  • 6. Re: Performace of StringBuilder - StringBuffer - which one is better
                    807603
                    How to improve your microbenchmark:

                    Execute more rounds. The computer's clock ticks fairly rarely; 10 ms
                    is common. If you run a loop for less than, say, 500 milliseconds
                    your results are mostly junk because they depend on when the clock
                    happened to tick.

                    Put the test in a separate method, then call that method several times
                    from main(). This allows Hotspot to compile the code.

                    Run with "java -server" if you are concerned about performance; that
                    will enable the optimizing Hotspot compiler. If you are not concerned
                    about performance ...why are you microbenchmarking in the first place?

                    When you do those things, you'll probably find that StringBuilder is
                    indeed faster. (Though I wonder if in the near future escape analysis
                    will be able to remove the synchronization present in StringBuffer,
                    making StringBuilder and StringBuffer equivalent in most cases...)

                    Try this (remember "java -server"!!!):
                    public class t
                    {
                        public static void main(String args[])
                        {
                            System.out.println("Ignore the first few timings.");
                            System.out.println("They may include Hotspot compilation time.");
                            System.out.println("I hope you are running me with \"java -server\"!");
                            for (int n = 0; n < 5; n++)
                                doit();
                            System.out.println("Did you run me with \"java -server\"? You should have.");
                            System.out.println("Forgetting \"-server\" makes baby Cthulhu cry.");
                        }
                    
                        public static void doit()
                        {
                         int num=10000;
                         int rounds = 1000;
                    
                         long t1=System.currentTimeMillis();
                         for (int n = 0; n < rounds; n++) {
                             StringBuffer sb = new StringBuffer("test");
                             for (int i=0;i<num;i++){
                              sb.append("x");
                             }
                         }
                         long buffertime = System.currentTimeMillis() -t1;
                         
                         long t2=System.currentTimeMillis();
                         for (int n = 0; n < rounds; n++) {
                             StringBuilder sb1 = new StringBuilder("test");
                             for (int i=0;i<num;i++){
                              sb1.append("x");
                             }
                         }
                         long buildertime = System.currentTimeMillis() -t2;
                         if(buildertime>buffertime)
                             System.out.println("Buffer is faster: " + buildertime + " vs " + buffertime);
                         else
                             System.out.println("Builder is faster: " + buildertime + " vs " + buffertime);
                        }
                    
                    }
                    • 7. Re: Performace of StringBuilder - StringBuffer - which one is better
                      807603
                      First ask yourself "will this performance hit make any difference to my application?"
                      • 8. Re: Performace of StringBuilder - StringBuffer - which one is better
                        796254
                        The javadocs tell you why StringBuilder is faster:

                        "This class provides an API compatible with StringBuffer, but with no guarantee of synchronization."

                        No monitors to acquire. Same reason why ArrayList will be faster than Vector.

                        %