我一直在尝试测量System.arrayCopy与Arrays.copyOf的性能,以便正确选择其中一个.仅仅为了基准测试我也添加了手动副本,结果让我感到惊讶.显然我错过了一些非常重要的东西,请你,告诉我,它是什么?实现如下(参见前4种方法).
public class ArrayCopy {
public static int[] createArray( int size ) {
int[] array = new int[size];
Random r = new Random();
for ( int i = 0; i < size; i++ ) {
array[i] = r.nextInt();
}
return array;
}
public static int[] copyByArraysCopyOf( int[] array, int size ) {
return Arrays.copyOf( array, array.length + size );
}
public static int[] copyByEnlarge( int[] array, int size ) {
return enlarge( array, size );
}
public static int[] copyManually( …Run Code Online (Sandbox Code Playgroud)