System.arrayCopy()和普通数组复制之间的效率差别

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

转载于:https://www.cnblogs.com/wellmaxwang/p/4499445.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值