都是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()还是有效率优势的, 但是感觉效率提升没有想象的大。

System.arrayCopy()和普通数组复制之间的效率差别的更多相关文章

  1. Java-Java中System.arraycopy() 和 Arrays.copyOf()两者之间的区别

    如果我们想拷贝一个数组,我们可能会使用System.arraycopy()或者Arrays.copyof()两种方式.在这里,我们将使用一个比较简单的示例来阐述两者之间的区别. 1.示例代码: Sys ...

  2. java的system.arraycopy()方法

    java.lang.System的静态方法arraycopy()可以实现数组的复制,讲课的老师说这个方法效率比较高,如果数组有成千上万个元素,那么用这个方法,比用for语句循环快不少.于是我试了试,发 ...

  3. 使用System.arraycopy()实现数组之间的复制

    System提供了一个静态方法arraycopy(),我们可以使用它来实现数组之间的复制. 其函数原型是: public static void arraycopy(Object src, int s ...

  4. 数组复制的五种方式(遍历循环一一赋值、System.arraycopy、地址赋值、克隆clone()、Arrays.copyof())

    package com.Summer_0424.cn; import java.util.Arrays; import java.util.concurrent.CopyOnWriteArrayLis ...

  5. [Java] System.arraycopy 数组复制

    函数原型: public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) ; s ...

  6. 数组的复制 --System.arraycopy()

    import java.util.Arrays; public class HellowWorld { public static void main(String[] argv ) { int[] ...

  7. Java数组的复制Arrays.copyOf()、System.arraycopy()、nums.clone()

    public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); a ...

  8. java System.arraycopy 数组复制和合并

    public class Test { public static void main(String[] args) { Integer[] a = {1,2,3}; Integer[] b = {4 ...

  9. Java性能漫谈-数组复制之System.arraycopy

    当我还年幼的时候,我很任性,复制数组也是,写一个for循环,来回倒腾,后来长大了,就发现了System.arraycopy的好处. 为了测试俩者的区别我写了一个简单赋值int[100000]的程序来对 ...

随机推荐

  1. Oculus安装问题

    1.必须FQ,可采用蓝灯,或其他vpn 2.Your computer doesn't meet Rift's recommended specifications 如果某些硬件达不到推荐配置(比如我 ...

  2. Callable与Future的介绍

    http://www.cnblogs.com/whgw/archive/2011/09/28/2194760.html

  3. instanceof 与isAssignableFrom

    instanceof 针对实例 isAssignableFrom针对class对象 isAssignableFrom   是用来判断一个类Class1和另一个类Class2是否相同或是另一个类的超类或 ...

  4. 关于SQLite的伪随机数

    --random() 的返回值范围是:[-2^63,(2^63)-1],而通常要的是最小为 0的随机数,于是就不能直接用了,解决办法: then col else -col end) result f ...

  5. HTML 或 CSS 文件中引用的图片文件移动到任意位置

    对于一个套接字的输入操作,第一步通常涉及等待数据从网络到达,当所等待分组到达时,被复制到内核的某个缓冲区: 第二步就是把数据从内核缓冲区复制到应用进程缓冲区.通过 Session 的 createQu ...

  6. qt 设置

    右击不同类型的文件,点属性,可以选择处理文件的工具,比如自动生成uic qrc右击工程属性,然后可以转换成QT工程翻译文件如果只读的话,可以右击换一个编辑器打开 如果翻译文件只读的话,必须直接使用字符 ...

  7. enable feature AJAX of MOSS2007

    As default, the feature AJAX of MOSS2007 is disabled, so the site web configuration file should be m ...

  8. asp.net发邮件功能

    protected void SendMail() { try { string CreaterName = ""; string examiner = ""; ...

  9. ajax的探究与使用

    前端必须掌握ajax,这是几乎所有前端招聘都会要求的一项. 但其实ajax也就是一种异步请求的技术,没有什么很深的东西,不过接触ajax很长一段时间了,早该整理下ajax的学习和使用: PART1: ...

  10. AppFog免费云空间申请及安装wordpress(图文教程)

    AppFog是一家提供运算平台的服务,用户可以在上面搭建自己的Web App.原本它的名字为PHPFog,但在采用了Cloud Foundry的代码作为核心,支持多个编程语言后,选择了更名.AppFo ...