Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String concat 성능 비교(StringBuilder를 쓰자) #58

Open
boojongmin opened this issue Jul 6, 2023 · 0 comments
Open

String concat 성능 비교(StringBuilder를 쓰자) #58

boojongmin opened this issue Jul 6, 2023 · 0 comments

Comments

@boojongmin
Copy link
Owner

소스

import org.springframework.util.StopWatch;

import java.util.Arrays;
import java.util.function.BooleanSupplier;

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

        var arr = new String[100_000];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = "" + Math.random();
        }

        var sw = new StopWatch();

        sw.start();
        var result = "";
        for (int i = 0; i < arr.length; i++) {
            result += arr[i];
        }
        sw.stop();
        System.out.println(">>> String +: " + sw.getLastTaskTimeMillis() + "ms");

        sw.start();
        var buffer = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            buffer.append(arr[i]);
        }
        result = buffer.toString();
        sw.stop();
        System.out.println(">>> StringBuffer without capacity: " + sw.getLastTaskTimeMillis() + "ms");

        sw.start();
        result = String.join("", arr);
        sw.stop();
        System.out.println(">>> String.join: " + sw.getLastTaskTimeMillis() + "ms");

        sw.start();
        var builder = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            builder.append(arr[i]);
        }
        result = builder.toString();
        sw.stop();
        System.out.println(">>> StringBuilder without capacity: " + sw.getLastTaskTimeMillis() + "ms");

        sw.start();
        builder = new StringBuilder(1_000_000);
        for (int i = 0; i < arr.length; i++) {
            builder.append(arr[i]);
        }
        result = builder.toString();
        sw.stop();
        System.out.println(">>> StringBuilder with capacity: " + sw.getLastTaskTimeMillis() + "ms");

        sw.start();
        buffer = new StringBuffer(1_000_000);
        for (int i = 0; i < arr.length; i++) {
            buffer.append(arr[i]);
        }
        result = buffer.toString();
        sw.stop();
        System.out.println(">>> StringBuffer with capacity: " + sw.getLastTaskTimeMillis() + "ms");

        sw.start();
        result = String.join("", arr);
        sw.stop();
        System.out.println(">>> String.join: " + sw.getLastTaskTimeMillis() + "ms");

        sw.start();
        result = "";
        for (int i = 0; i < arr.length; i++) {
            result += arr[i];
        }
        sw.stop();
        System.out.println(">>> String +: " + sw.getLastTaskTimeMillis() + "ms");


    }
}

결과

(핫스팟 영향이 있음)

String +: 10173ms
StringBuffer without capacity: 9ms
String.join: 13ms
StringBuilder without capacity: 2ms
StringBuilder with capacity: 2ms
StringBuffer with capacity: 3ms
String.join: 4ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant