码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-05-13 10:07:08      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

都是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()和普通数值复制之间的效率差别

标签:

原文地址:http://www.cnblogs.com/wellmaxwang/p/4499445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!