Speed comparison test for String, StringBuilder, StringBuffer

-A String object is created every time a string is concatenated using the String class.

-It is said that the StringBuilder and StringBuffer classes should be used because this is an overhead.

・ I don't use these classes, saying, "That's not a big overhead." Of course you should use it if you have a lot of times, but (to me) there aren't many such situations.

-When using these classes, it is a little problem that the source code becomes difficult to see.

・ I measured how much it was different.

-Roughly speaking, it takes more than 10 times when directly operating String. -However, since the absolute time is not large in the first place, if the number of times is small, it is considered that the String direct operation is sufficient.

-Please use it for judgment when using StringBuffer (StringBuilder).

-When it reaches 1 million times, StringBuffer is faster than StringBuilder. The reason is unknown (to me).

・ Here, the result is shown once, but the tendency is the same no matter how many times it is done.

-Difference between StringBuffer and StringBuilder. The former is thread-safe and the latter is not thread-safe. The latter is new.

・ The reason why I didn't make it thread-safe is that it is rarely used in threads, and I don't want to slow down because of that (Imagine).

-If you want to use StringBuilder in a thread, you should control it yourself.

Tips0016.java


package jp.avaj.lib.algo;

import java.util.Date;

import jp.avaj.lib.test.L;

class Tips0016 {
  private static int CNT0;
  private static final int CNT1 = 100; //
  public static void main(String[] args) {
    L.p("====10,000 times");
    CNT0 = 10000;
    execString();
    execStringBuilder();
    execStringBuffer();

    L.p("====100,000 times");
    CNT0 = 100000;
    execString();
    execStringBuilder();
    execStringBuffer();

    L.p("====Million times");
    CNT0 = 1000000;
    execString();
    execStringBuilder();
    execStringBuffer();

  }

  private static void execString() {
    long start = (new Date()).getTime();
    for (int i=0; i<CNT0; i++) {
      String s = "";
      for (int j=0; j<CNT1; j++) {
        s += "aaa";
      }
    }
    long end = (new Date()).getTime();
    diff("String",start,end);
  }
  private static void execStringBuilder() {
    long start = (new Date()).getTime();
    for (int i=0; i<CNT0; i++) {
      StringBuilder sb = new StringBuilder();
      for (int j=0; j<CNT1; j++) {
        sb.append("aaa");
      }
    }
    long end = (new Date()).getTime();
    diff("StringBuilder",start,end);
  }
  private static void execStringBuffer() {
    long start = (new Date()).getTime();
    for (int i=0; i<CNT0; i++) {
      StringBuffer sb = new StringBuffer();
      for (int j=0; j<CNT1; j++) {
        sb.append("aaa");
      }
    }
    long end = (new Date()).getTime();
    diff("StringBuffer",start,end);
  }

  private static void diff(String desc,long start,long end) {
    L.p(desc+"="+(end-start));
  }
}

The execution result is as follows.

Tips0016.txt


====10,000 times
String=230
StringBuilder=31
StringBuffer=52
====100,000 times
String=1383
StringBuilder=149
StringBuffer=352
====Million times
String=12336
StringBuilder=1433
StringBuffer=1388

Recommended Posts

Speed comparison test for String, StringBuilder, StringBuffer
[Java] String join execution speed comparison (+ operator vs StringBuilder)
MyBatis string comparison
String and stringbuffer and string builder
Java --StringBuilder vs StringBuffer