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]的程序来对 ...
随机推荐
- ActiveReports中如何在后台导出运行时绑定数据源报表
ActiveReports支持运行时绑定数据源功能,这种绑定数据源方法使用较为普及,然而很多系统中都需要在后台导出报表文件,所以用户就很困惑,ActiveReports中如何在后台导出运行时绑定数据源 ...
- 使用Jmeter进行HTTP接口测试
这几天学习了一下使用Jmeter进行HTTP接口测试,这个也是后面进行性能测试的基础吧 安装运行请自行搜索,下载地址:http://jmeter.apache.org/download_jmeter. ...
- Balanced Binary Tree [LeetCode]
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 学习PYTHON之路, DAY 6 - PYTHON 基础 6 (模块)
一 安装,导入模块 安装: pip3 install 模块名称 导入: import module from module.xx.xx import xx from module.xx.xx impo ...
- UEditor手动调节其宽度
其高度一般不考虑,给个初始高度,然后任其自动扩展就行,对于其宽度,有两种思路,一种是调节其所在的DIV的宽度,让其自动填充,另一种是直接调节编辑器的宽度: adjust_editor_size: fu ...
- 将centos7打造成桌面系统
前言以下所有操作默认在root权限下执行,桌面环境是kde,使用gnome的也可以参考一下.我收集的以下要用到的一些安装包,360网盘http://yunpan.cn/csMhBAp92vTgN 提取 ...
- TypeError: window.open is not a function
想必大家现在都已经到家了,而苦逼的我还要坐在办公室混拿微薄的工资,技不如人,平常不努力给自己充电,年终一毛钱都没多给.不说这扫兴的话题了,在这给同样在苦逼坚守岗位的同志们节日的问候,新的一年,好运连连 ...
- WCF初探文章列表
WCF初探-1:认识WCF WCF初探-6:WCF服务配置 WCF初探-2:手动实现WCF程序 WCF初探-7:WCF服务配置工具使用 WCF初探-3:WCF消息交换模式之单向模式 WCF初探-8:W ...
- CentOS6.4下基于Nginx搭建mp4/flv流媒体服务器
我的步骤如下:1. 安装依赖包: yum install glibc.i686#yum –y update#yum -y install gcc glibc glibc-devel make nasm ...
- jquery mobile 的优缺点
jQuery Mobile 优点 跨浏览器兼容性最好,几乎兼容所有的平台和浏览器: 入门简单,语法简洁,编码灵活,一些简单的应用直接用HTML既可实现,无需Javascript: 开源插件与第三方扩展 ...