62,621
社区成员
发帖
与我相关
我的任务
分享 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;
byte[] des = new byte[2];
for (int i = 0; i < 100000; i++) {
System.arraycopy(bytes, 2, des, 0, 2);
string =Arrays.toString(des);
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();
System.out.println(end - start);
}
次数改成10000000了,你那个方法要2s多,这个600多ms
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);package pkg;
public class TestBytes {
/**
* @param args
*/
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
byte[] bytes = {1, 2,
97, 98, 97, 98, 97, 98, 97, 98, 97, 98,
97, 98, 97, 98, 97, 98, 97, 98, 97, 98,
97, 98, 97, 98, 97, 98, 97, 98, 97, 98,
97, 98, 97, 98, 97, 98, 97, 98, 97, 98,};
System.out.print(foo1(sb, bytes));
sb.delete(0, sb.length());
System.out.print(",");
System.out.print(foo2(sb, bytes));
sb.delete(0, sb.length());
System.out.print(",");
System.out.print(foo3(sb, bytes));
sb.delete(0, sb.length());
System.out.print(",");
System.out.print(foo4(sb, bytes));
sb.delete(0, sb.length());
System.out.println();
}
/**
* @param sb
* @param bytes
*/
private static long foo4(StringBuilder sb, byte[] bytes) {
long start = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
for (int j = 0; j < bytes.length - 2; j++) {
sb.append(bytes[j + 2]);
}
}
long end = System.currentTimeMillis();
return end - start;
}
/**
* @param sb
* @param bytes
*/
private static long foo3(StringBuilder sb, byte[] bytes) {
long start = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
char[] chars = new char[40];
for (int j = 0; j < chars.length; j++) {
chars[j] = (char) bytes[j + 2];
}
sb.append(chars);
}
long end = System.currentTimeMillis();
return end - start;
}
/**
* @param sb
* @param bytes
*/
private static long foo2(StringBuilder sb, byte[] bytes) {
long start = System.currentTimeMillis();
String string = null;
for (int i = 0; i < 100000; i++) {
string = new String(bytes, 2, 40);
sb.append(string);
}
long end = System.currentTimeMillis();
return end - start;
}
/**
* @param sb
* @param bytes
*/
private static long foo1(StringBuilder sb, byte[] bytes) {
long start = System.currentTimeMillis();
String string = "abababababababababababababababababababab";
for (int i = 0; i < 100000; i++) {
sb.append(string);
}
long end = System.currentTimeMillis();
return end - start;
}
}
结果为
62,110,15,140