[Note] Java: Measures the speed of string concatenation


I've always remembered that it's faster to do String-type character concatenation with the StringBuilder append from my superior at the previous site, but the reference book I'm studying now also mentions that. So I wanted to practice how fast it really is.


public class StringBuilderTest {
    public static void main(String[] args) {

        String name = "";
        long start = System.currentTimeMillis();
        for(int i=1; i<=1000; i++){
            name += "tanaka, ";
        long end = System.currentTimeMillis();
        System.out.println("+Linking: "+(end - start)  + "ms");

        StringBuilder sb = new StringBuilder();
        long start2 = System.currentTimeMillis();
        for(int i=1; i<=400000; i++){
            sb.append("tanaka, ");
        String name2 = sb.toString();
        long end2 = System.currentTimeMillis();
        System.out.println("StringBuilder.append concatenation: "+(end2 - start2)  + "ms");


        +Linking: 24ms
        StringBuilder.append concatenation: 27ms

As you can see from the number of times the for statement is turned, it takes 24ms to execute 1000 times for concatenation with +, and append concatenation is executed 400,000 times with 27ms. Even if you only execute it a few times, it's worth remembering that the previous site also ran the for statement in units of 10,000.

Recommended Posts

[Note] Java: Measures the speed of string concatenation
[Java] Speed comparison of string concatenation
Item 63: Beware the performance of string concatenation
The story of low-level string comparison in Java
[Java] Get the length of the surrogate pair string
[Java] The confusing part of String and StringBuilder
[Note] Java: String search
[Note] Java: String survey
[Note] Java: Speed of List processing by purpose
Please note the division (division) of java kotlin Int and Int
[Note] Java Output of the sum of odd and even elements
[Java] Try editing the elements of the Json string using the library
[Java] Delete the elements of List
Various methods of Java String class
[Java version] The story of serialization
Note on the path of request.getRequestDispatcher
Various methods of the String class
[Note] Handling of Java decimal point
[Java] Correct comparison of String type
I read the source of String
The origin of Java lambda expressions
Handling of java floating point [Note] while reading the reference book
The story of not knowing the behavior of String by passing Java by reference
I tried to summarize the methods of Java String and StringBuilder
How to check for the contents of a java fixed-length string
Get the result of POST in Java
Java string
Check the contents of the Java certificate store
Examine the memory usage of Java elements
Memo: [Java] Check the contents of the directory
Compare the elements of an array (Java)
Basic knowledge of Java development Note writing
[day: 5] I summarized the basics of Java
What are the updated features of java 13
Easily measure the size of Java Objects
Looking back on the basics of Java
[Java] How to get to the front of a specific string using the String class
Output of the book "Introduction to Java"
[Java] Comparison of String type character strings
The story of writing Java in Emacs
[Java] Check the number of occurrences of characters
[Java] Check if the character string is composed only of blanks (= Blank)
[Java] [Spring] Test the behavior of the logger
Handling of java floating point [Note] while reading the reference book
[Java] How to easily get the longest character string of ArrayList using stream
String # split (String regex, int limit) Note on the operation specifications of the second argument
[Java] Handling of JavaBeans in the method chain
JAVA: jar, aar, view the contents of the file
The story of making ordinary Othello in Java
[Android] [Java] Manage the state of CheckBox of ListView
About the description order of Java system properties
About the idea of anonymous classes in Java
The order of Java method modifiers is fixed
Compare the speed of the for statement and the extended for statement.
[Java] Access the signed URL of s3 (signed version 2)
The story of learning Java in the first programming
Measure the size of a folder in Java
I compared the characteristics of Java and .NET
[Java] Be careful of the key type of Map
Feel the passage of time even in Java
Calculate the similarity score of strings with JAVA