日期:2014-05-20 浏览次数:20821 次
public class ArrayCopy { public static void main(String[] args) { int size =1024*1024*5; int i,length = size; char oldArray[] = new char[size]; char newArray[] = new char[size]; for(i=0;i<size;i++) oldArray[i]=(char) i; long startTime = System.currentTimeMillis(); for(i=0;i<1000;i++) { selfCopy(oldArray, 0, newArray, 0, length); //100次2475 1000次19290 //System.arraycopy(oldArray, 0, newArray, 0, length);// 940 10225 //newArray=oldArray.clone(); // 2545 25425 //selfCopy(newArray, 0, newArray, 1, length-1); //System.arraycopy(newArray, 0, newArray, 1, length-1); } long endTime = System.currentTimeMillis(); System.out.println("消耗时间: " + (endTime - startTime)+" ms."); System.out.println(new String(oldArray).substring(100,200)+"\n\n"+new String(newArray).substring(100,200)); } private static void selfCopyObj(Object oldArray, int startPosOld, Object newArray, int startPosNew, int length) { for (int i = 0; i < length; i++) { ( (Object[] )newArray)[startPosNew++] = ( (Object[]) oldArray)[startPosOld++]; } } private static void selfCopy(char[] oldArray, int startPosOld, char[] newArray, int startPosNew, int length) { for (int i = 0; i < length; i++) { newArray[startPosNew++] = oldArray[startPosOld++]; } } }