머리말
최근에 문자열 스 플라이 싱을 고려하고 몇 가지 방법을 생각했을 수도 있지만 성능은 알려지지 않았으므로 아래에서 테스트 해 봅시다. 자세한 소개를 함께 살펴 보겠습니다.
샘플 코드
공개 클래스 테스트 {list <string> list = new ArrayList <> (); @public void init () {intstream.range (0, 100000) .foreach ((index) -> {list.add ( "str" + index);}); } @org.junit.test public void test1 () {String ss = ""; Long StartTime = System.CurrentTimeMillis (); for (문자열 s : list) {ss += s; } system.out.println (System.CurrentTimeMillis () -StartTime); } @org.junit.test public void test2 () {String ss = ""; Long StartTime = System.CurrentTimeMillis (); for (문자열 s : list) {ss = ss.concat (s); } system.out.println (System.CurrentTimeMillis () -StartTime); } @org.junit.test public void test3 () {StringBuilder ss = new StringBuilder (); Long StartTime = System.CurrentTimeMillis (); for (문자열 s : list) {ss.Append (s); } system.out.println (System.CurrentTimeMillis () -StartTime); } @org.junit.test public void test4 () {long starttime = system.currenttimeMillis (); StringUtils.join (List); System.out.println (System.CurrentTimeMillis () -StartTime); } @org.junit.test public void test5 () {StringBuffer ss = new StringBuffer (); Long StartTime = System.CurrentTimeMillis (); for (문자열 s : list) {ss.Append (s); } system.out.println (System.CurrentTimeMillis () -StartTime); }}첫 번째 유형 : 33809
두 번째 유형 : 8851
세 번째 유형 : 6
네 번째 유형 : 12
다섯 번째 유형 : 7
성능 : StringBuilder>StringBuffer>StringUtils.join>concat>+
그런 다음 소스 코드 수준에서 분석하십시오
StringBuilder :
각 문자열 스 플라이 싱은 내부 숯 어레이를 확장하고 최종 문자열 만 생성하므로 가장 효율적입니다.
StringBuffer :
StringBuilder와 비교할 때 동기화 된 것이 하나만 있으므로 단일 스레드 케이스에서는 차이가 크지 않습니다.
StringUtils.join :
StringBuilder와 함께 내부적으로 구현되어 있음을 알 수 있지만 각 루프에는 판단 할 여분의 분리기가 있으므로 약간 느리지 만별로는 절차입니다.
CONCAT :
각 연결이 문자열이 생성되므로 효율이 매우 낮습니다.
+:
오버로드 된 연산자이기 때문에 소스 코드를 찾을 수 없지만 결과에 따라 효율이 가장 낮습니다.
요약
위는이 기사의 전체 내용입니다. 이 기사의 내용에 모든 사람의 연구 나 작업에 대한 특정 참조 가치가 있기를 바랍니다. 궁금한 점이 있으면 의사 소통을 위해 메시지를 남길 수 있습니다. Wulin.com을 지원 해주셔서 감사합니다.