【java performance】使用'System.arraycopy ()'代替通过来循环复制数组

'System.arraycopy ()' 要比通过循环来复制数组快的多。

        

例子:

public class IRB

{

   void method () {

       int[] array1 = new int [100];

       for (int i = 0; i < array1.length; i++) {

           array1 [i] = i;

       }

       int[] array2 = new int [100];

       for (int i = 0; i < array2.length; i++) {

           array2 [i] = array1 [i];                 // Violation

       }

    }

}

        

更正:

public class IRB

{

   void method () {

       int[] array1 = new int [100];

       for (int i = 0; i < array1.length; i++) {

           array1 [i] = i;

       }

       int[] array2 = new int [100];

       System.arraycopy(array1, 0, array2, 0, 100);

    }

}

        

参考资料:

http://www.cs.cmu.edu/~jch/java/speed.html

你可能感兴趣的:(【java performance】使用'System.arraycopy ()'代替通过来循环复制数组)