日期:2014-05-20 浏览次数:20844 次
StringBuilder sb = new StringBuilder();
long start = System.currentTimeMillis();
byte[] bytes = {1,2,3,4};
for (int i = 0; i < 100000; i++) {
String string = new String(bytes, 2, 2);
// String string = "test";
sb.append(string);
}
long end = System.currentTimeMillis();
System.out.println(end - start);
StringBuilder sb = new StringBuilder();
long start = System.currentTimeMillis();
byte[] bytes = {1,2,3,4};
String string=null;
for (int i = 0; i < 100000; i++) {
string = new String(bytes, 2, 2);
sb.append(string);
}
long end = System.currentTimeMillis();
System.out.println(end - start);
public static void main(String[] args) {
long start = System.currentTimeMillis();
byte[] bytes = { 1, 2, 3, 4 };
byte[] target = new byte[1024 * 1024];
int currentIndex = 0;
for (int i = 0; i < 10000000; i++) {
if(currentIndex + 2 > target.length){
byte[] newTarget = new byte[target.length * 3 / 2];
System.arraycopy(target, 0, newTarget, 0, currentIndex);
target = newTarget;
}
System.arraycopy(bytes, 2, target, currentIndex, 2);
currentIndex += 2;
}
String str = new String(target, 0, currentIndex);
System.out.println(str.length());
long end = System.currentTimeMillis();