You are on page 1of 3

REFERENCE CARD

CONTENTS INCLUDE: . String Concatenation Operation . Performance Comparison . Summary . Source Code

String Concatenation Benchmark


By Leonar Tambunan

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()

String Concatenation Performance Benchmark

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

String Concatenation Performance Benchmark


Average elapsed time in nanoseconds
25000

20000

String Concatenation StringBuffer StringBuilder

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

String Concatenation Performance Benchmark


Average elapsed time in nanoseconds
20000

String Concatenation StringBuffer StringBuilder

15000

10000

5000

0 2 4 6 8 101214161820222426 262830323436384042444648505254565860626466687072 7274767880828486889092949698

Number of loops

60000

String Concatenation Performance Benchmark


Average elapsed time in nanoseconds
50000

40000

String Concatenation StringBuffer StringBuilder

String Concatenation Performance Benchmark

30000

20000

10000

0 1 2 3 4 5 6 7 8 9 10111213 131415161718192021222324252627282930313233343536 363738394041424344454647484950

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

SOURCE CODE String Concatenation +


String str; long start; int x = 50; for (int n = 1; n < x ; n++) { str = ""; start = System.nanoTime(); for (int i=0;i<n; i++) { str = str + "abcde"; } long elapsed = System.nanoTime() start; System.out.print(n); System.out.print("\t"); System.out.println(elapsed / n); }

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); }

You might also like