62,614
社区成员
发帖
与我相关
我的任务
分享
public class ClassTest1 {
public final String getName() {
return name;
}
public final void setName(String name) {
this.name = name;
}
private String name;
}
public class ClassTest2 {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
}
public static void main(String[] args) {
ClassTest1 ct1=new ClassTest1();
ClassTest2 ct2=new ClassTest2();
Long t1=System.currentTimeMillis();
for (int i = 0; i <1000000 ; i++) {
ct1.getName();
}
Long t2=System.currentTimeMillis();
for (int i = 0; i <1000000 ; i++) {
ct2.getName();
}
Long t3=System.currentTimeMillis();
System.out.println("add final decorate cost time:"+(t2-t1));
System.out.println("not add final decorate cost time:"+(t3-t2));
}
String x = "";
for (int i = 0; i <1000000 ; i++) {
x+= ct1.getName();
}
Long t2=System.currentTimeMillis();
System.out.println("add final decorate cost time:"+(t2-t1));
x="";
t1 = =System.currentTimeMillis();
for (int i = 0; i <1000000 ; i++) {
ct2.getName();
}
t2 = =System.currentTimeMillis();
System.out.println("not add final decorate cost time:"+(t2-t1));
每次都是带final的快,快50%,除非我继承一个ClassTest1,那么这个测试会比 ClassTest2慢一倍