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

无意间看到一篇文章其中说到: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对性能影响的度么?(待续。。。)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值