您的位置:首页 > 其它

final修饰函数在性能上提高的测试。

2012-03-09 14:11 375 查看
无意间看到一篇文章其中说到:final 修饰函数可以提升性能

于是我找到这种提升性能的2种解释:

其一:

final方法:

当一个方法用“final”来修饰时,表示该方法不可以被子类重写。

使用final方法的原因有二:   

一、限制了子类中对其改写;

二、提高了执行的效率,因为这种情况属于静态绑定,Java虚拟机(JVM)的即时编译器就不会去检索该方法在其父类、子类或爷爷类、孙子类等有内联关系的类中是否存在重写,省去了动态判断究竟要执行哪一个类(具体内联关系的类)的这个名称的方法。

其二:

final方法   如果一个类不允许其子类覆盖某个方法,则可以把这个方法声明为final方法。   

使用final方法的原因有二:   

第一、把方法锁定,防止任何继承类修改它的意义和实现。   

第二、高效。编译器在遇到调用final方法时候会转入内嵌机制,大大提高执行效率。
按照我的理解:相当于C++ 中 inline 函数

于是,编写我的测试实例:

测试一:非继承情况下

package cn.vicky.chapt12;

/**
*
* @author Vicky.H
*/
public class FinalTest {

static long temp;

/**
* 测试方法一
*/
public void Method1(int i) {
temp = i + 1;
temp = i + 2;
temp = i + 3;
temp = i + 4;
temp = i + 5;
temp = i + 6;
temp = i + 7;
temp = i + 8;
temp = i + 9;
temp = i + 10;
}

/**
* 测试方法二
*/
public final void Method2(int i) {
temp = i + 1;
temp = i + 2;
temp = i + 3;
temp = i + 4;
temp = i + 5;
temp = i + 6;
temp = i + 7;
temp = i + 8;
temp = i + 9;
temp = i + 10;
}

public static void main(String args[]) {
FinalTest finalTest = new FinalTest(); // 2个函数在执行时间上几乎相同
long start, now;
start = System.currentTimeMillis();
for (int i = 0; i < 999999999; i++) {
finalTest.Method1(i);
}
now = System.currentTimeMillis() - start;
System.err.println("调用不带final关键字的Method1方法耗时(ms毫秒):" + now);
System.out.println("---------------------------------------------------");
start = System.currentTimeMillis();
for (int i = 0; i < 999999999; i++) {
finalTest.Method2(i);
}
now = System.currentTimeMillis() - start;
System.err.println("调用带final关键字的Method2方法耗时(ms毫秒):" + now);
}
}


这种情况下,final与非final几乎没有差异。

测试二:在继承情况下

package cn.vicky.chapt12;

/**
*
* @author Vicky.H
*/
public class FinalParent {

static long temp;

/**
* 测试方法一
*/
public void Method1(int i) {
temp = i + 1;
temp = i + 2;
temp = i + 3;
temp = i + 4;
temp = i + 5;
temp = i + 6;
temp = i + 7;
temp = i + 8;
temp = i + 9;
temp = i + 10;
}

/**
* 测试方法二
*/
public final void Method2(int i) {
temp = i + 1;
temp = i + 2;
temp = i + 3;
temp = i + 4;
temp = i + 5;
temp = i + 6;
temp = i + 7;
temp = i + 8;
temp = i + 9;
temp = i + 10;
}
}


package cn.vicky.chapt12;

/**
* 注意,该测试
* @author Vicky.H
*/
public class FinalChild extends FinalParent {

/**
* 测试程序入口方法
* @param args 入口参数列表
*/
public static void main(String[] args) {
// FinalChild finalTest = new FinalChild();
FinalParent finalTest = new FinalChild();
long start, now;
start = System.currentTimeMillis();
for (int i = 0; i < 999999999; i++) {
finalTest.Method1(i);
}
now = System.currentTimeMillis() - start;
System.err.println("调用不带final关键字的Method1方法耗时(ms毫秒):" + now);
System.out.println("---------------------------------------------------");
start = System.currentTimeMillis();
for (int i = 0; i < 999999999; i++) {
finalTest.Method2(i);
}
now = System.currentTimeMillis() - start;
System.err.println("调用带final关键字的Method2方法耗时(ms毫秒):" + now);

}
}


但任然同样的,函数在执行时间上几乎相同,那么我可以认为是因为我的代码快达不到final对性能影响的度么?(待续。。。)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: