最近正在看ThinkInJavaIII,上面说arraycopy用来考贝数组数据会比自己直接写代码快得多。本来我已经默认了这个结果。但是昨天稍微作了一点测试,
本来是想看看到底会快多少,但是发现结果却是直接写代码反而还快一点,不解!
这是测试代码: 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));
}
}输出结果为:
Loop directly=515
b[]=[250,250,250,250,250,250,250,250,250,250]
System.arraycopy=610
b[]=[250,250,250,250,250,250,250,250,250,250]注:Arrays2.toString(b))是把数组串成字符串。另外我的机器配制大概为:
CPU:(Intel Core2 Duo T8100 2.10GHZ)X2
Memory: 2G
本来是想看看到底会快多少,但是发现结果却是直接写代码反而还快一点,不解!
这是测试代码: 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));
}
}输出结果为:
Loop directly=515
b[]=[250,250,250,250,250,250,250,250,250,250]
System.arraycopy=610
b[]=[250,250,250,250,250,250,250,250,250,250]注:Arrays2.toString(b))是把数组串成字符串。另外我的机器配制大概为:
CPU:(Intel Core2 Duo T8100 2.10GHZ)X2
Memory: 2G
解决方案 »
- 面试被问到 如何遍历一个map
- com.mchange.v2.c3p0.ComboPooledDataSource找不到
- linux下运行jar文件报错未找到资源文件
- 如何判断jtable中的数据修改了还是添加了?
- Dialog中如何操作主窗口中的panel
- N处错误咋改呀
- Timer 小程序问题??????需要大家帮忙???
- 请问如果用java编写像本地磁盘中的大图标这种文件夹形式界面...急
- 如何去除JDialog中右上角的起关闭作用的那个叉?
- 请问Random类的Random r=new Random()和Random r=new Random(seedValue)有什么区别?
- GetResultCount什么函数?
- 帮忙看看,为什么无法达到预期效果
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));
}
}结果:
Loop directly=500
System.arraycopy=47
猜想:
应该是Java在用下标访问的时候,加上了边界检查,呵呵
增加其内部存储容量的。