[JAVA] [String type vs String Builder] Difference in processing speed in string concatenation

Introduction

When concatenating strings in Java, there are two ways to connect them, either by connecting them with + = in String type or by connecting them with ʻappend ()` of StringBuilder.

The results are the same for both, but there is a difference in processing speed.

When I did it with String

For example, this code

String stringResult = null;
    //String concatenation with a String object
    for (int i = 0; i < 10; i++) {
      String str = Integer.toString(i);
      stringResult += str;
    }

Since the String type is a String object, it means that a String type variable is declared = a String object is created.

Therefore, the String object is created at the timing of stringResult + = str; which is concatenation of strings.

In other words, String objects are created for the number of loops in the for statement.

When done with StringBuilder

For example, this code

StringBuilder sb = new StringBuilder();
    //String concatenation with StringBuilder
    for (int j = 0; j < 10; j++) {
      sb.append(Integer.toString(j));
    }

The result is the same as the String example.

However, ʻappend ()` does not create a String object. Just add it to the end.

Processing is faster because the String object is not created.

How different is the speed?

I measured the difference in processing time between String and StringBuilder with the following program.

public class StringBuilderSample{
  public static void main(String[] args) {
    final int MAX_COUNT = 100;
    System.out.println("Number of joins:" + MAX_COUNT + "When");
    /*********************For String type*********************/
    String stringResult = null;

    //Time taken to process with String
    long sTime;

    //Start time
    long startTime = System.nanoTime();

    //String concatenation by String type
    for (int i = 0; i < MAX_COUNT; i++) {
      String str = Integer.toString(i);
      stringResult += str;
    }

    //ending time
    long endTime = System.nanoTime();

    //Calculate the time taken for processing
    sTime = endTime - startTime;

    System.out.println("Processing time in String:" + sTime + "Nanoseconds");

    /*********************For StringBuilder*********************/
    StringBuilder sb = new StringBuilder();

    //Time taken by StringBuilder
    long sbTime;

    //Start time
    startTime = System.nanoTime();

    //String concatenation with StringBuilder
    for (int j = 0; j < MAX_COUNT; j++) {
      sb.append(Integer.toString(j));
    }

    //ending time
    endTime = System.nanoTime();

    //Calculate the time taken for processing
    sbTime = endTime - startTime;

    System.out.println("Processing time in StringBuilder:" + sbTime + "Nanoseconds");
    System.out.println("StringBuilder is better" + (sTime - sbTime) + "Nanoseconds fast");
  }
}

It is a program that displays the processing speed when string concatenation is performed as many times as MAX_COUNT.

Below, we will look at the difference in processing time while increasing the value of MAX_COUNT to 100, 1000, 10,000, 100,000.

By the way, nanoseconds are 10 ^ (-9) seconds (= 0.000000001 seconds).

When executed 100 times

スクリーンショット 2018-07-19 23.11.48.png

When executed 1000 times

スクリーンショット 2018-07-19 23.12.23.png

When executed 10,000 times

スクリーンショット 2018-07-19 23.13.53.png About 0.57 seconds faster

When executed 100,000 times

スクリーンショット 2018-07-19 23.14.49.png

About 27 seconds faster

StringBuilder was faster!

It doesn't bother me so much because the difference is up to about 1000 times in microseconds (10 ^ (-6)) at most.

From around 10,000 times, the difference in processing time becomes anxious.

Well, it's true that StringBuilder is faster no matter how many times you run it, so unless you have a specific reason, you should do string concatenation with StringBuilder.

at the end

Actually, this article is a reprint from the blog. I also post about other things on my blog.

** Scribbles of system engineers-Hatena Blog **

2018 07/21 Addendum: I wrote a sequel → [java] Do not use "+" in append!

Recommended Posts

[String type vs String Builder] Difference in processing speed in string concatenation
[Java] Speed comparison of string concatenation
[Java] Difference between equals and == in a character string that is a reference type
[Note] Java: Measures the speed of string concatenation
Spring 4.2 vs 4.3: Difference in bind value to Collection type when submitting check box empty