都是System.arrayCopy() 效率高,到底有多高呢,拉出来遛遛就知道了:
package JCF.ArrayList;
import java.util.Date;
public class ArrayCopyCompare {
public static void main(String[] args) {
int length = 1000000;
//init
System.out.println("array length : "+length);
int[] array = new int[length];
for(int i = 0 ; i < array.length ; i ++){
array[i] = i;
}
//use method by system
long begin1 = new Date().getTime();
int[] arrayCopyBySystem = new int[length];
System.arraycopy(array, 0, arrayCopyBySystem, 0, array.length);
long end1 = new Date().getTime();
System.out.println("use time by system method : "+(end1 - begin1));
//use method normal
long begin2 = new Date().getTime();
int[] arrayCopyByNormal = new int[length];
for(int i = 0 ; i < arrayCopyByNormal.length ; i ++){
arrayCopyByNormal[i] = array[i];
}
long end2 = new Date().getTime();
System.out.println("use time by narmal method : " +(end2 - begin2));
}
}
常用的数组复制方法就按照循环赋值的防范, 下面列举有不多长度数组的效率比较:
array length : 10000 use time by system method : 0 use time by narmal method : 0
array length : 100000 use time by system method : 0 use time by narmal method : 2
array length : 1000000 use time by system method : 3 use time by narmal method : 5
array length : 10000000 use time by system method : 25 use time by narmal method : 29
array length : 100000000 use time by system method : 279 use time by narmal method : 293
所以在数组长度不大时,两者效率可以忽略, 但是数组长度变大时,System.arrayCopy()还是有效率优势的, 但是感觉效率提升没有想象的大。