Java String Concatenation Benchmark
Java String Concatenation Benchmark
CONTENTS INCLUDE: . String Concatenation Operation . Performance Comparison . Summary . Source Code
STRING CONCATENATION OPERATION Java String Java String is the most used immutable class in Java. Immutable means cant be changed or mutated. All of the Java String operations are using create-new new and throw-old throw strategy. String str = hello; str = str.toUppercase(); This snippet of code does not modify value of object that variable str refers to. . Instead, a new String object is created with all the characters of str is changed to upper case. The old hello string will be garbage collected since no more variable refers to it. String Concatenation String concatenation operation can be achieved using, using Concatenation operation + to strings StringBuffer.append() StringBuilder.append()
Concatenation operation + to Java Strings S is done by creating new string and the old string thrown away. Concatenation operation using StringBuffer.append and StringBuilder.append is done by appends - using native code System.arraycopy() - the new string to an array of chars containing the old string. StringBuilder is compatible API with StringBuffer but with no guarantee of synchronization.
PERFORMANCE COMPARISON Charts below are the performance comparison of mentioned concatenation operations operations above by appending abcde string in a loop. This benchmark is done using Java 6 Hotspot with JIT enabled. enable X-axis is the number of loops; Y-axis axis is the average elapsed time per operation.
30000
20000
15000
10000
5000
0 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 260 270 280 290
Number of loops
Reference Card
Performance Comparison continued
25000
15000
10000
5000
Number of loops
60000
40000
30000
20000
10000
Number of loops
SUMMARY 1. For small number of iterations iteration ( <40 ) shown in third chart, the performance of StringBuffer & StringBuilder doesnt t really significant despite the spikes of + concatenation performance. But above that number of iterations, StringBuffer & StringBuilder does over perform the + concatenation concatenatio 2. There is no significant difference performance between Non thread-safe safe StringBuilder and thread-safe StringBuffer. 3. The champion of string concatenation is StringBuffer
ABOUT THE AUTHOR Leonar Tambunan is a software engineer, java performance tuning enthusiast. enthusiast Leonard.tambunan@gmail.com
StringBuffer
StringBuffer str; long start; int x = 50; for (int n = 1; n < x ; n=n+1) { str = new StringBuffer(); start = System.nanoTime(); for (int i=0;i<n; i++) { str.append("abcde"); } long elapsed = System.nanoTime() start; System.out.print(n); System.out.print("\t"); System.out.println(elapsed / n); }
StringBuilder
StringBuilder str; long start; int x = 50; for (int n = 1; n < x ; n++) { str = new StringBuilder(); start = System.nanoTime(); for (int i=0;i<n; i++) { str.append("abcde"); } long elapsed = System.nanoTime() - start; System.out.print(n); System.out.print("\t"); System.out.println(elapsed / n); }