Wednesday, 11 May 2016

Java Tutorial : Java String vs StringBuffer vs StringBuilder(Performance)


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=IWRh7OVJPbA&list=UUhwKlOVR041tngjerWxVccw

PerformanceTest.java
public class PerformanceTest
{

    /*
     * String is slow and consumes more memory when you
     * concat too many strings because every time it creates
     * new instance.
     */
    public static String concatenatingWithString()
    {
        String str = "Welcome";
        for (int i = 0; i < 70000; i++)
        {
            str = str + "Peter";
        }
        return str;
    }

    /*
     * StringBuffer is fast and consumes less memory when
     * you cancat strings.
     * 
     * StringBuffer is faster than String.
     */
    public static String concatenatingWithStringBuffer()
    {
        StringBuffer sb = new StringBuffer("Welcome");
        for (int i = 0; i < 70000; i++)
        {
            sb.append("Peter");
        }
        return sb.toString();
    }

    /*
     * StringBuilder is fast and consumes less memory when
     * you cancat strings.
     * 
     * StringBuilder is faster than StringBuffer.
     */
    public static String concatenatingWithStringBuilder()
    {
        StringBuilder sb = new StringBuilder("Welcome");
        for (int i = 0; i < 70000; i++)
        {
            sb.append("Peter");
        }
        return sb.toString();
    }

    public static void main(String[] args)
    {
        long startTime = System.currentTimeMillis();

        concatenatingWithString();

        System.out
                .println("Time taken by Concatenating with String       : "
                        + (System.currentTimeMillis() - startTime)
                        + "ms");

        startTime = System.currentTimeMillis();

        concatenatingWithStringBuffer();

        System.out
                .println("Time taken by Concatenating with StringBuffer :  "
                        + (System.currentTimeMillis() - startTime)
                        + "ms");

        startTime = System.currentTimeMillis();

        concatenatingWithStringBuilder();

        System.out
                .println("Time taken by Concatenating with StringBuilder: "
                        + (System.currentTimeMillis() - startTime)
                        + "ms");
    }
}
Output
Time taken by Concatenating with String       : 17394ms
Time taken by Concatenating with StringBuffer :  20ms
Time taken by Concatenating with StringBuilder: 10ms
Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/StringBuilderDemo_Perf_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/StringBuilderDemo_Perf_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/c539eeaf4c6ff331d8e2aa2333826ea70c999093/BasicJava/StringBuilderDemo_Perf_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment