1700452031
1700452032
}catch(CloneNotSupportedException e){
1700452033
1700452034
throw new Error();
1700452035
1700452036
}
1700452037
1700452038
}}
1700452039
1700452040
public static void main(String[]args){
1700452041
1700452042
//循环10万次
1700452043
1700452044
final int maxLoops=10*10000;
1700452045
1700452046
int loops=0;
1700452047
1700452048
//开始时间
1700452049
1700452050
long start=System.nanoTime();
1700452051
1700452052
//“母”对象
1700452053
1700452054
Apple apple=new Apple();
1700452055
1700452056
while(++loops<maxLoops){
1700452057
1700452058
apple.clone();
1700452059
1700452060
}
1700452061
1700452062
long mid=System.nanoTime();
1700452063
1700452064
System.out.println(“clone方法生成对象耗时:”+(mid-start)+“ns”);
1700452065
1700452066
//new生成对象
1700452067
1700452068
while(—loops>0){
1700452069
1700452070
new Apple();
1700452071
1700452072
}
1700452073
1700452074
long end=System.nanoTime();
1700452075
1700452076
System.out.println(“new生成对象耗时:”+(end-mid)+“ns”);
1700452077
1700452078
}
1700452079
1700452080
在上面的代码中,Apple是一个简单的可拷贝类,用两种方式生成了10万个苹果:一种是通过克隆技术,一种是通过直接种植(也就是new关键字),按照我们的常识想当然地会认为克隆肯定比new要快,但是结果却是这样的:
[
上一页 ]
[ :1.700452031e+09 ]
[
下一页 ]