Wednesday, January 22, 2014

Java - String

public class Test {

public static void main(String[] args) {
final int LOOP_COUNT = 10000;
final String str = "test";
String a = new String();
StringBuffer b = new StringBuffer(); // thread-safe
StringBuilder c = new StringBuilder(); // thread-not-safe

long startTime = System.nanoTime();

for (int i = 0; i < LOOP_COUNT; i++) {
a += str;
}

long endTime = System.nanoTime();
double elaspedTime = (endTime - startTime) / 1000000.0;

System.out.println(elaspedTime);

startTime = System.nanoTime();

for (int i = 0; i < LOOP_COUNT; i++) {
b.append(str);
}

b.toString();

endTime = System.nanoTime();
elaspedTime = (endTime - startTime) / 1000000.0;

System.out.println(elaspedTime);

startTime = System.nanoTime();

for (int i = 0; i < LOOP_COUNT; i++) {
c.append(str);
}

c.toString();

endTime = System.nanoTime();
elaspedTime = (endTime - startTime) / 1000000.0;

System.out.println(elaspedTime);
}
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.