Java性能漫谈-数组复制之System.arraycopy
当我还年幼的时候,我很任性,复制数组也是,写一个for循环,来回倒腾,后来长大了,就发现了System.arraycopy的好处。
为了测试俩者的区别我写了一个简单赋值int[100000]的程序来对比,并且中间使用了nanoTime来计算时间差:
程序如下:
int[] a = new int[100000];
for(int i=0;i<a.length;i++){
a[i] = i;
} int[] b = new int[100000]; int[] c = new int[100000];
for(int i=0;i<c.length;i++){
c[i] = i;
} int[] d = new int[100000]; for(int k=0;k<10;k++){
long start1 = System.nanoTime();
for(int i=0;i<a.length;i++){
b[i] = a[i];
}
long end1 = System.nanoTime();
System.out.println("end1 - start1 = "+(end1-start1)); long start2 = System.nanoTime();
System.arraycopy(c, 0, d, 0, 100000);
long end2 = System.nanoTime();
System.out.println("end2 - start2 = "+(end2-start2)); System.out.println();
}
为了避免内存不稳定干扰和运行的偶然性结果,我在一开始的时候把所有空间申明完成,并且只之后循环10次执行,得到如下结果:
end1 - start1 = 366806
end2 - start2 = 109154 end1 - start1 = 380529
end2 - start2 = 79849 end1 - start1 = 421422
end2 - start2 = 68769 end1 - start1 = 344463
end2 - start2 = 72020 end1 - start1 = 333174
end2 - start2 = 77277 end1 - start1 = 377335
end2 - start2 = 82285 end1 - start1 = 370608
end2 - start2 = 66937 end1 - start1 = 349067
end2 - start2 = 86532 end1 - start1 = 389974
end2 - start2 = 83362 end1 - start1 = 347937
end2 - start2 = 63638
可以看出,System.arraycopy的性能很不错,为了看看究竟这个底层是如何处理的,我找到openJDK的一些代码留恋了一些:
System.arraycopy是一个native函数,需要看native层的代码:
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
找到对应的openjdk6-src/hotspot/src/share/vm/prims/jvm.cpp,这里有JVM_ArrayCopy的入口:
JVM_ENTRY(void, JVM_ArrayCopy(JNIEnv *env, jclass ignored, jobject src, jint src_pos,
jobject dst, jint dst_pos, jint length))
JVMWrapper("JVM_ArrayCopy");
// Check if we have null pointers
if (src == NULL || dst == NULL) {
THROW(vmSymbols::java_lang_NullPointerException());
}
arrayOop s = arrayOop(JNIHandles::resolve_non_null(src));
arrayOop d = arrayOop(JNIHandles::resolve_non_null(dst));
assert(s->is_oop(), "JVM_ArrayCopy: src not an oop");
assert(d->is_oop(), "JVM_ArrayCopy: dst not an oop");
// Do copy
Klass::cast(s->klass())->copy_array(s, src_pos, d, dst_pos, length, thread);
JVM_END
前面的语句都是判断,知道最后的copy_array(s, src_pos, d, dst_pos, length, thread)是真正的copy,进一步看这里,在openjdk6-src/hotspot/src/share/vm/oops/typeArrayKlass.cpp中:
void typeArrayKlass::copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) {
assert(s->is_typeArray(), "must be type array"); // Check destination
if (!d->is_typeArray() || element_type() != typeArrayKlass::cast(d->klass())->element_type()) {
THROW(vmSymbols::java_lang_ArrayStoreException());
} // Check is all offsets and lengths are non negative
if (src_pos < || dst_pos < || length < ) {
THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
}
// Check if the ranges are valid
if ( (((unsigned int) length + (unsigned int) src_pos) > (unsigned int) s->length())
|| (((unsigned int) length + (unsigned int) dst_pos) > (unsigned int) d->length()) ) {
THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
}
// Check zero copy
if (length == )
return; // This is an attempt to make the copy_array fast.
int l2es = log2_element_size();
int ihs = array_header_in_bytes() / wordSize;
char* src = (char*) ((oop*)s + ihs) + ((size_t)src_pos << l2es);
char* dst = (char*) ((oop*)d + ihs) + ((size_t)dst_pos << l2es);
Copy::conjoint_memory_atomic(src, dst, (size_t)length << l2es);//还是在这里处理copy
}
这个函数之前的仍然是一堆判断,直到最后一句才是真实的拷贝语句。
在openjdk6-src/hotspot/src/share/vm/utilities/copy.cpp中找到对应的函数:
// Copy bytes; larger units are filled atomically if everything is aligned.
void Copy::conjoint_memory_atomic(void* from, void* to, size_t size) {
address src = (address) from;
address dst = (address) to;
uintptr_t bits = (uintptr_t) src | (uintptr_t) dst | (uintptr_t) size; // (Note: We could improve performance by ignoring the low bits of size,
// and putting a short cleanup loop after each bulk copy loop.
// There are plenty of other ways to make this faster also,
// and it's a slippery slope. For now, let's keep this code simple
// since the simplicity helps clarify the atomicity semantics of
// this operation. There are also CPU-specific assembly versions
// which may or may not want to include such optimizations.) if (bits % sizeof(jlong) == ) {
Copy::conjoint_jlongs_atomic((jlong*) src, (jlong*) dst, size / sizeof(jlong));
} else if (bits % sizeof(jint) == ) {
Copy::conjoint_jints_atomic((jint*) src, (jint*) dst, size / sizeof(jint));
} else if (bits % sizeof(jshort) == ) {
Copy::conjoint_jshorts_atomic((jshort*) src, (jshort*) dst, size / sizeof(jshort));
} else {
// Not aligned, so no need to be atomic.
Copy::conjoint_jbytes((void*) src, (void*) dst, size);
}
}
上面的代码展示了选择哪个copy函数,我们选择conjoint_jints_atomic,在openjdk6-src/hotspot/src/share/vm/utilities/copy.hpp进一步查看:
// jints, conjoint, atomic on each jint
static void conjoint_jints_atomic(jint* from, jint* to, size_t count) {
assert_params_ok(from, to, LogBytesPerInt);
pd_conjoint_jints_atomic(from, to, count);
}
继续向下查看,在openjdk6-src/hotspot/src/cpu/zero/vm/copy_zero.hpp中:
static void pd_conjoint_jints_atomic(jint* from, jint* to, size_t count) {
_Copy_conjoint_jints_atomic(from, to, count);
}
继续向下查看,在openjdk6-src/hotspot/src/os_cpu/linux_zero/vm/os_linux_zero.cpp中:
void _Copy_conjoint_jints_atomic(jint* from, jint* to, size_t count) {
if (from > to) {
jint *end = from + count;
while (from < end)
*(to++) = *(from++);
}
else if (from < to) {
jint *end = from;
from += count - ;
to += count - ;
while (from >= end)
*(to--) = *(from--);
}
}
可以看到,直接就是内存块赋值的逻辑了,这样避免很多引用来回倒腾的时间,必然就变快了。
Java性能漫谈-数组复制之System.arraycopy的更多相关文章
- Java 数组拷贝方法 System.arraycopy
System类提供的数组拷贝方法: public static native void arraycopy(Object src, int srcPos, Object dest, int destP ...
- jdk提供的数组扩容方法:System.arraycopy
package chapter7; /* * jdk提供的扩容方法 * System.arraycopy */public class TestArrayjdk { public static voi ...
- [Java] arraycopy 数组复制(转)
public class ArraycopyTest { public static void main(String[] args) { //静态初始化两个长度不同的 ...
- 【Java基础】System.arraycopy()的使用详解
由于在Java中System.arraycopy()方法在一维数组和二维数组中的表现不同,所以做了一个测试 public static void main(String[] args) { int[] ...
- java.lang.System.arraycopy() 与java.util.Arrays.copyOf()的区别
java.lang.System.arraycopy() 与java.util.Arrays.copyOf()的区别 一.java.lang.System.arraycopy() 该方法的声明: /* ...
- java System.arrayCopy使用说明
java System.arrayCopy使用说明 java.lang.System.arraycopy() 方法复制指定的源数组的数组,在指定的位置开始,到目标数组的指定位置. 下面是 System ...
- 【java】为数组全部元素赋同样的值 以及 数组之间的复制
为数组全部元素赋同样的值 : boolean[] resArray=new boolean[100]; Arrays.fill(resArray, true); 数组之间的复制: System.arr ...
- java中的数组概念
数组的定义形式: 动态初始化方式: 1.声明并开辟数组 String str[]=new String[3];//3表示数组的长度 2.分布完成 String str[]=null; str=new ...
- [Java] System.arraycopy 数组复制
函数原型: public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) ; s ...
随机推荐
- DG下手工处理v$archive_gap方法
从9i以后,oracle dataguard 备库一般都不需要手工处理丢失的日志,FAL自动会帮我们处理,下面通过个案例来讲下手工处理丢失的日志的方法: 1.在备库查询有哪些日志丢失,没应用到备库 S ...
- iOS_SN_百度地图基本使用(1)
上次用了一次百度地图,一直没有记笔记,今天记一笔. 以前没有用过百度地图的时候,听做这方面的朋友说百度地图有不少的坑,但是我做的时候没有遇到太大的坑,主要是要注意官方文档的注意事项,还有配置环境开发中 ...
- EasyUi DataGrid中数据编辑方式及编辑后数据获取,校验处理
EasyUi中的DataGrid提供前台编辑的相关函数. 实现客户选中DataGrid中一列后,对选中列中的一个字段进行编辑,并对数据进行实时校验后,传递至后台保存的需求, 主要涉及到DataGrid ...
- (转)yum 和 apt-get 用法及区别
原地址:http://www.cnblogs.com/adforce/archive/2013/04/12/3017577.html 一般来说著名的linux系统基本上分两大类: 1 RedHat系 ...
- php 之 数据访问 增删改查
一.建立主页面: <title>主页面</title> </head> <body> <h1>主页面</h1> <tabl ...
- 解决APP中fragment重叠问题
由于内存重启,导致的frgament重叠,其原因就是FragmentState没有保存Fragment的显示状态,即mHidden,导致页面重启后,该值为默认的false,即show状态,所以导致了F ...
- zip命令的用法
语法zip [参数] [打包后的文件名] [打包的目录路径] 参数列表-a 将文件转成ASCII模式-F 尝试修复损坏的压缩文件-h 显示帮助界面-m 将文件压缩之后,删除源文件-n 特定字符串,不压 ...
- 关于开源中文搜索引擎架构coreseek中算法详解
Coreseek 是一款中文全文检索/搜索软件,以GPLv2许可协议开源发布,基于Sphinx研发并独立发布,专攻中文搜索和信息处理领域,适用于行业/垂直搜索.论坛/站内搜索.数据库搜索.文档/文献 ...
- 对JAVA的static深刻理解(结合C语言的思考)
public class statictest { String X = "我是非静态变量"; static int butterfly =0; static String sta ...
- Mysql 新建用户以及授权远程连接操作
1:以root身份登陆mysql终端 mysql -uroot -pmysql 2:创建wx用户,注意密码要加单引号 mysql> create user wx identified by 'w ...