System.arraycopy比手工的for复制速度慢?
现在用的是jdk6u7。在数据量很小的情况下,手动生成会更快。但当数组超过20项左右时,System.arraycopy()更快。当数组长度为数百时,System.arraycopy()的优势相当明显。此时clone()方法的速度也相当显眼。
如果使用-server模式,性能将提高近一倍。现在每个人的机器内存都很大。当使用java平台工具时,您可能会添加-server。
Java版本" 1.6.0 _ 07"
Java (TM) SE运行时环境(Build 1 . 6 . 0 _ 07-B06)
Java Hotspot(TM)客户端VM (Build 10.0-B23,混合模式,共享结果中数字的单位是毫秒。
-
prompt > Java arraycopy test
短数组# # # # # # # # # # # # # # # # #
array clone 1328
arrayManulCopy 266
array copy 359
长数组# # # # # # # # # # # # # # # # # # # # # # # # # # # #
array clone 1359
arrayManulCopy 469
arraySysCopy 406
array copy 391
海量数组############ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 1828
arrayManulCopy 4250
array copy 1875
海量数组# # # # # # # # # # # #
导入Java . util . random;
公共类ArrayCopyTest {
公共静态void main(String[]args){
new ArrayCopyTest()。array copy();
}
Object[]srcarray 1 = { " sdfsdaf ",new Date(),new Integer(200)};
Object[]srcarray 2 =
{ " qwerasd ",new Date(),new Integer(190),srcArray1,new Object(),
new String[] { "aaa "," bbb " } }
Object[]srcarray 3 = new Object[30];
Object[]srcarray 4 = new Object[150];
Object[]srcarray 5 = new Object[300];
ArrayCopyTest(){
fill array(srcarray 3);
fill array(srcarray 4);
fill array(srcarray 5);
}
/**
*填充对象数组。
* @param objs填充目标。
* private void fill array(Object[]objs){
for(int I = 0;i < objs.lengthi = i + 10) {
fillArray(objs,I);
}
}
void fill array(Object[]objs,int start){
objs[start++]= new Object();
objs[start++]= new Date();
objs[start++]= getRandomString();
objs[start++]= getRandomString();
objs[start++]= getRandomString();
objs[start++]= getRandomString();
objs[start++]= getRandomString();
objs[start++]= r . nextlong();
objs[start++]= getRandomString();
objs[start++]= getRandomString();
}
私有Random r = new Random();
/**
*获取一个随机字符串。
* @ return random string
*/
string get random string(){
string buffer sb = new string buffer();
int loop = 3+r . nextint(4);
while(loop > 0){
loop-;
sb . append((char)(97+r . nextint(25)));
sb . append((char)(65+r . nextint(25)));
}
return sb . tostring();
}
void array copy(){
test method(" short array \ t # # # # # # # # # # # # ",srcarray 1);
test method(" long array \ t # # # # # # # # # # # # # # # # # # ",srcarray 2);
testMethod("大数组\t######################### ",
srcarray 3);
testMethod("巨大数组\ t # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ",
srcarray 4);
test method(" mass array \ t # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ",
srcarray 5);
}
/**
*组测试。
* @param prmpt测试提示
* @param args测试对象数组
*/
void测试方法(string prmpt,object[]args){
int loop = 500000;
system . out . println(prmpt);
arrayClone(loop,args);
arrayManualCopy(loop,args);
array copy(loop,args);
}
void array clone(int loop,Object[]source array){
long time = system . current time millis();
for(int I = 0;I <循环;++ I){
Object[]destArray =(Object[])source array . clone();
}
system . out . println(" array clone \ t "+
(system . current time millis()-time));
}
void array copy(int loop,Object[]source array){
long time = system . current time millis();
for(int I = 0;I <循环;++ I){
Object[]destArray = new Object[source array . length];
system . array copy(source array,0,destArray,0,source array . length);
}
system . out . println(" array copy \ t "+
(system . current time millis()-time));
}
void array manual copy(int loop,Object[]source array){
long time = system . current time millis();
for(int I = 0;I <循环;++ I){
Object[]destArray = new Object[source array . length];
for(int j = 0;j < sourceArray.length++ j){
destArray[j]= source array[j];
}
}
system . out . println(" arrayManulCopy \ t "+
(system . current time millis()-time));
}
}
0条评论