zoukankan      html  css  js  c++  java
  • 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万次跑下来,并未和普通调用有多大差距

  • 相关阅读:
    C#编程.异常处理(Exception Handling Statements)
    rabbitMQ 消息队列
    非技术问题
    sql优化
    数控机床,模具
    nginx 服务器
    zuul 网关
    分布式配置中心
    hystrix 解决服务雪崩效应
    负载均衡
  • 原文地址:https://www.cnblogs.com/whatlonelytear/p/5081524.html
Copyright © 2011-2022 走看看