I ran the following JUnit test case and was able to continuously get good performance results for Stringbuffer than StringBuilder. I'm sure that I'm missing something here but I could not find the reason behind why I get better speed for StringBuffer than StringBuilder.
My test case is,
    @Test
    public void stringTest(){
        String s1 = "s1";
        String s2 = "s2";
        for(int i=0;i<=100000;i++){
            s1 = s1+s2;
        }
        System.out.println(s1);
    }
    @Test
    public void stringBuilderTest(){
        StringBuilder s1 = new StringBuilder("s1");
        String s2 = "s2";
        for(int i=0;i<=100000;i++){
            s1.append(s2);
        }
        System.out.println(s1);
    }
    @Test
    public void stringBufferTest(){
        StringBuffer s1 = new StringBuffer("s1");
        String s2 = "s2";
        for(int i=0;i<=100000;i++){
            s1.append(s2);
        }
        System.out.println(s1);
    }
Please find the JUnit test results,
as you can see in the above results stringBufferTest case has executed sooner than stringBuilderTest case. My question is why is this? I know this is impossible theoretically but how I'm getting this result?
Update
As per the @Henry's comment I removed the SysOuts and results got changed dramatically.
Then I increase the loop count by 100000 -> 1000000 and was able to get some realistic results which I expected all the time,
Well my new questions are,
- Why I get a significant performance improvement when I remove my SysOut?
- When the load is increased low to high in 1000000 StringBuffer gives the best results over StringBuilder, why is that?




 
     
    