Today I decided to test different ways of concatenation. I chose concatenation with "+" operator, String.format(...) and StringBuilder. I wrote simple code for determining time in milliseconds for each test.
Source Code:
package test;
import java.util.Iterator;
import java.util.TreeSet;
public class Main {
private static final double A = 300d;
private static final long B = 80087L;
private static final int C = 7897;
private static final char D = '5';
private static final boolean E = true;
private static final String F = "Text Text Text";
public static void main(String[] args) throws InterruptedException {
// Warming up begin
Thread.sleep(10000);
test1();
test2();
test3();
// Warming up end
long test1 = test1();
long test2 = test2();
long test3 = test3();
System.out.println(String.format(
"Test #1 (concat): %d\nTest #2 (String.format): %d\nTest #3 (StringBuilder): %d\n",
test1, test2, test3));
}
private static long test1() {
long tbegin = System.currentTimeMillis();
String str = "";
for (long i = 0; i < 5000; i++) {
str += "a = " + A + "\nb = " + B + "\nc = " + C
+ "\nd = " + D + "\ne = " + E + "\nf = " + F + "\n";
}
long tend = System.currentTimeMillis();
System.out.println(str);
return tend - tbegin;
}
private static long test2() {
long tbegin = System.currentTimeMillis();
String str = "";
for (long i = 0; i < 5000; i++) {
str = String.format("%sa = %f\nb = %d\nc = %d\nd = %c\ne = %b\nf = %s\n",
str, A, B, C, D, E, F);
}
long tend = System.currentTimeMillis();
System.out.println(str);
return tend - tbegin;
}
private static long test3() {
long tbegin = System.currentTimeMillis();
StringBuilder strb = new StringBuilder();
for (long i = 0; i < 5000; i++) {
strb.append("a = ").append(A);
strb.append("\nb = ").append(B);
strb.append("\nc = ").append(C);
strb.append("\nd = ").append(D);
strb.append("\ne = ").append(E);
strb.append("\nf = ").append(F).append("\n");
}
String str = strb.toString();
long tend = System.currentTimeMillis();
System.out.println(str);
return tend - tbegin;
}
}
Results:
Test #1 (concat): 10734 Test #2 (String.format): 12516 Test #3 (StringBuilder): 16 Test #1 (concat): 10515 Test #2 (String.format): 12500 Test #3 (StringBuilder): 32 Test #1 (concat): 10485 Test #2 (String.format): 12515 Test #3 (StringBuilder): 31
So the best result has test #3 (StringBuilder), the second one is concatenation with "+" operator and the worst one is String.format. The leadership of StringBuilder was expected for me, but out of the blue next to it is plain concatenation! The point is that plain concatenation uses StringBuilder. It's what JavaDoc says about this:
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder (or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.
The last one, String.format is very charming way which allows you to write easy-readable code. Unfortunately it's too slow, so it might be used carefully. But it's still so cool, so I'd rather use it unless very loaded parts of code.
Comments