java数组的四种拷贝方法的性能分析:for、clone、System.arraycopy、Arrays.copyof
2017-07-11 14:34
806 查看
今天作者就Java数组的拷贝方法进行性能比较,多有不足,请大家指正!!!
1.for方法,用循环依次实现数组元素的拷贝。
2.clone方法,是Object类的方法,用来实现对象的复制操作。
3.System.arraycopyof方法,是System的静态方法,是一个native方法,用来实现数组的复制操作,提供了多种重载方法,大家可以去看源码了解了解。
4.Arrays.copyof方法,是Arrays工具类的静态方法,其底层调用System.arraycopy方法,也提供了多种重载方法,用来实现数组的复制操作。
源码分析:
运行结果:
当我把size改为10时,运行结果如下:
当我把size改为100时,运行结果如下:
当我把size改为1000时,运行结果如下:
也就是说,当数组元素个数不是很大时,使用for来复制数组,时间性能是最好的,clone次之,其次是System.arraycopy,最差的是Arrays.copyof。
当数组元素个数很大时,使用System.arraycopy复制数组,时间性能是最好的,clone次之,其次是Arrays.copyof,最差的就是for。
1.for方法,用循环依次实现数组元素的拷贝。
2.clone方法,是Object类的方法,用来实现对象的复制操作。
3.System.arraycopyof方法,是System的静态方法,是一个native方法,用来实现数组的复制操作,提供了多种重载方法,大家可以去看源码了解了解。
4.Arrays.copyof方法,是Arrays工具类的静态方法,其底层调用System.arraycopy方法,也提供了多种重载方法,用来实现数组的复制操作。
源码分析:
package JavaNukeTest; import java.util.Arrays; public class ArrayCopyTest { public static final int size = 10000; public static void copyByClone(String[] strArray){ long startTime = System.nanoTime(); String[] destArray = strArray.clone(); long endTime = System.nanoTime(); System.out.println("copyByClone cost time is "+(endTime - startTime)); } public static void copyByLoop(String[] strArray){ long startTime = System.nanoTime(); String[] destArray = new String[size]; for (int i = 0; i < strArray.length; i++){ destArray[i] = strArray[i]; } long endTime = System.nanoTime(); System.out.println("copyByLoop cost time is "+(endTime - startTime)); } public static void copyByArrayCopy(String[] strArray){ long startTime = System.nanoTime(); String[] destArray = new String[size]; System.arraycopy(strArray, 0, destArray, 0, strArray.length); long endTime = System.nanoTime(); System.out.println("copyByArrayCopy cost time is "+(endTime - startTime)); } public static void copyByCopyof(String[] strArray){ long startTime = System.nanoTime(); String[] destArray = Arrays.copyOf(strArray, strArray.length); long endTime = System.nanoTime(); System.out.println("copyByCopyof cost time is "+(endTime - startTime)); } public static void main(String[] args) { // TODO Auto-generated method stub String[] strArray = new String[size]; for (int i = 0; i < size; i++){ strArray[i] = "abc"; } copyByClone(strArray); copyByLoop(strArray); copyByArrayCopy(strArray); copyByCopyof(strArray); } }
运行结果:
当我把size改为10时,运行结果如下:
当我把size改为100时,运行结果如下:
当我把size改为1000时,运行结果如下:
也就是说,当数组元素个数不是很大时,使用for来复制数组,时间性能是最好的,clone次之,其次是System.arraycopy,最差的是Arrays.copyof。
当数组元素个数很大时,使用System.arraycopy复制数组,时间性能是最好的,clone次之,其次是Arrays.copyof,最差的就是for。
相关文章推荐
- java数组的拷贝四种方法:for、clone、System.arraycopy、arrays.copyof
- java数组的拷贝四种方法:for、clone、System.arraycopy、arrays.copyof
- [置顶] Java数组 以及 System.arraycopy() 的拷贝分析
- 数组拷贝 Array.copyof()方法与 System.arraycopy()方法
- [java]数组复制小结 for 、clone、 System.arraycopy
- Java数组的复制Arrays.copyOf()、System.arraycopy()、nums.clone()
- Java方法之--System.arraycopy方法和Arrays.copyOf()
- 关于Java 拷贝数组方法 Arrays.copyOf() 是地址传递还是值传递
- Java方法之--System.arraycopy方法和Arrays.copyOf()
- Java数组的复制Arrays.copyOf()、System.arraycopy()、nums.clone()
- [java]数组复制小结 for 、clone、 System.arraycopy
- Java方法之--System.arraycopy方法和Arrays.copyOf()
- Java方法-System.arraycopy方法和Arrays.copyOf()
- Java方法System.arraycopy方法和Arrays.copyOf()
- Java编程:Arrays.copyOf()与System.arraycopy()的分析
- 在Java中System.arraycopy() 和 Arrays.copyOf()两者之间的区别?
- 2017/12/31Java基础学习——复制数组の——Arrays.copyOfRange()方法讲解
- 在Java中System.arraycopy() 和 Arrays.copyOf()两者之间的区别
- Java数组复制的方法和System.arraycopy
- 数组的拷贝(System.arraycopy和clone)