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()还是有效率优势的, 但是感觉效率提升没有想象的大。
System.arrayCopy()和普通数组复制之间的效率差别的更多相关文章
- Java-Java中System.arraycopy() 和 Arrays.copyOf()两者之间的区别
如果我们想拷贝一个数组,我们可能会使用System.arraycopy()或者Arrays.copyof()两种方式.在这里,我们将使用一个比较简单的示例来阐述两者之间的区别. 1.示例代码: Sys ...
- java的system.arraycopy()方法
java.lang.System的静态方法arraycopy()可以实现数组的复制,讲课的老师说这个方法效率比较高,如果数组有成千上万个元素,那么用这个方法,比用for语句循环快不少.于是我试了试,发 ...
- 使用System.arraycopy()实现数组之间的复制
System提供了一个静态方法arraycopy(),我们可以使用它来实现数组之间的复制. 其函数原型是: public static void arraycopy(Object src, int s ...
- 数组复制的五种方式(遍历循环一一赋值、System.arraycopy、地址赋值、克隆clone()、Arrays.copyof())
package com.Summer_0424.cn; import java.util.Arrays; import java.util.concurrent.CopyOnWriteArrayLis ...
- [Java] System.arraycopy 数组复制
函数原型: public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) ; s ...
- 数组的复制 --System.arraycopy()
import java.util.Arrays; public class HellowWorld { public static void main(String[] argv ) { int[] ...
- Java数组的复制Arrays.copyOf()、System.arraycopy()、nums.clone()
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); a ...
- java System.arraycopy 数组复制和合并
public class Test { public static void main(String[] args) { Integer[] a = {1,2,3}; Integer[] b = {4 ...
- Java性能漫谈-数组复制之System.arraycopy
当我还年幼的时候,我很任性,复制数组也是,写一个for循环,来回倒腾,后来长大了,就发现了System.arraycopy的好处. 为了测试俩者的区别我写了一个简单赋值int[100000]的程序来对 ...
随机推荐
- 了解Android的编译器
了解一下Android的编译器并记录下来: Android在4.4以前是使用Dalvik VM的,通过Just In Time(JIT即时编译)来完成编译工作,在Android4.4提供了一种测试版本 ...
- CSS3 选择器——基本选择器
CSS的选择器,我想大家并不会陌生吧,因为天天在使用,但对于CSS3的选择器,要运用的灵活到位,我想对很多朋友还是一定的难度,特别是CSS3中的:nth选择器.那么从现在开始我们先丢开他们版本的区别, ...
- Python压缩
ru=lambda x:x.decode('u8') rp=lambda x:x.replace('\\','/') gb=lambda x:x.decode('gbk') class ZIP: de ...
- mysql 5.7 忘记root密码解决方法
1.修改配置文件 vim /etc/my.cnf 在[mysqld]节点添加 skip-grant-tables 2.重启mysql 3.用空密码进入 mysql -uroot 执行 update m ...
- FullPage.js – 轻松实现全屏滚动(单页网站)效果
FullPage.js 是一个简单而易于使用的插件,用来创建全屏滚动网站(也被称为单页网站).除了可以创建全屏滚动效果以外,也可以给网站添加一些水平的滑块效果.能够自适应不同的屏幕尺寸,包括平板电脑和 ...
- 计时器js
<html> <head> <meta http-equiv="Content-Type" content="text/html ...
- jquery mobile 实现自定义confirm确认框效果
类似删除的效果,在执行之前,一般需要添加确认对话框,点确认的话执行,取消按钮就不执行,传统的js if(confirm('确定删除吗?')) { //执行代码 } 这种效果比较丑,使用jquery m ...
- SAP SD 销售凭证如何设置字段必填
在实际业务中,我们经常遇到需要设置某些字段是必输的.那么在SAP中创建销售订单时如何控制必填字段呢?请看操作手册 第一步:设置屏幕增强 T-CODE:shd0 上截图 1----输入需要控制的事物代码 ...
- ListView
ListView:列表展示数据1.视图 - 在其右上方小箭头点击将视图改为Largelcon:或右键属性在外观View将其改为Details2.设置列头 - 在其右上方小箭头点击选择编辑列,然后添加列 ...
- vpn是什么?手机vpn是什么?
vpn是什么?手机vpn是什么? vpn是什么?手机vpn是什么? vpn是什么?手机vpn是什么?VPN全称是Virtual Private Network,也就是虚拟专用网的意思.很多人不知道VP ...