62,610
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) {
// testfill();
int a[] = new int[10];
Arrays.fill(a, 250);
long start, end, times;
times = 10000000;
start = System.currentTimeMillis();
{
int b[] = new int[10];
for (int i = 0; i < times; i++) {
for (int j = 0; j < b.length; j++)
b[j] = a[j];
}
end = System.currentTimeMillis();
System.out.println("Loop directly=" + (end - start));
System.out.println("b[]=" + Arrays2.toString(b));
}
start = System.currentTimeMillis();
{
int b[] = new int[10];
for (int i = 0; i < times; i++) {
System.arraycopy(a, 0, b, 0, b.length);
}
end = System.currentTimeMillis();
System.out.println("System.arraycopy=" + (end - start));
System.out.println("b[]=" + Arrays2.toString(b));
}
}
private static void testArrayCopyRate() {
int size = 1000;
double a[] = new double[size];
Arrays.fill(a, 250);
long start, end, times;
times = 100000;
start = System.currentTimeMillis();
{
double b[] = new double[size];
for (int i = 0; i < times; i++) {
for (int j = 0; j < b.length; j++)
b[j] = a[j];
}
end = System.currentTimeMillis();
System.out.println("Loop directly=" + (end - start));
//System.out.println("b[]=" + Arrays2.toString(b));
}
start = System.currentTimeMillis();
{
double b[] = new double[size];
for (int i = 0; i < times; i++) {
System.arraycopy(a, 0, b, 0, b.length);
}
end = System.currentTimeMillis();
System.out.println("System.arraycopy=" + (end - start));
//System.out.println("b[]=" + Arrays2.toString(b));
}
}