jdk1.6 反射性能对比【原】

ReflectPerformance.java

 

package aaa.bbb.ccc;

import java.lang.reflect.Method;

public class ReflectPerformance {
    String name ;
    
    
    public static void reflect(String arg){
        int  i = 0 ;
    }
    
    
    public static void one() throws Exception{
        TLTimeContainer.recordTime();
        
        Method m ;
        ReflectPerformance obj = new ReflectPerformance();
        for(int i = 0 ; i < 100000 ; i ++){
            m = obj.getClass().getMethod("reflect",  new Class[]{String.class});
            m.invoke(obj, "bobo");
        }
        
        TLTimeContainer.recordTime();
        TLTimeContainer.print();
        TLTimeContainer.clearAll();
    }
    
    public static void two() throws Exception{
        TLTimeContainer.recordTime();
        
        Method m ;
        ReflectPerformance obj = new ReflectPerformance();
        for(int j = 1 ; j < 100000 ; j ++){
            obj.reflect("bobo");
        }
        
        TLTimeContainer.recordTime();
        TLTimeContainer.print();
        TLTimeContainer.clearAll();
    }
    
    public static void main(String[] args) throws Exception{
        one();
        two();
    }
}

 

打印结果

类aaa.bbb.ccc.ReflectPerformance->方法one 起于:1453857415270
类aaa.bbb.ccc.ReflectPerformance->方法one 止于:1453857415364
类aaa.bbb.ccc.ReflectPerformance->方法one 耗时:94
__________________________________________
类aaa.bbb.ccc.ReflectPerformance->方法two 起于:1453857415365
类aaa.bbb.ccc.ReflectPerformance->方法two 止于:1453857415366
类aaa.bbb.ccc.ReflectPerformance->方法two 耗时:1
__________________________________________

 

TLTimeContainer源码.

 

总结:10万次跑下来,并未和普通调用有多大差距

posted @ 2015-12-28 09:10  苦涩泪滴  阅读(365)  评论(0编辑  收藏  举报