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万次跑下来,并未和普通调用有多大差距

相关文章:

  • 2022-12-23
  • 2021-06-15
  • 2022-12-23
  • 2021-08-11
  • 2022-03-07
  • 2022-01-29
  • 2021-06-06
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-25
  • 2021-10-25
  • 2022-12-23
  • 2022-12-23
  • 2021-09-09
相关资源
相似解决方案