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

  • 相关阅读:
    IDEA如何打包可运行jar的一个问题。
    从一个脚本谈loadrunner的脚本初始化
    explain 用法详解
    linux zip 命令详解
    Jmeter使用——参数化
    Jmeter Constant Throughput Timer 使用
    产生随机时间的例子
    Mysql的列索引和多列索引(联合索引)
    Loadrunner负载机agent
    Spring context:property-placeholder 一些坑
  • 原文地址:https://www.cnblogs.com/whatlonelytear/p/5081524.html
Copyright © 2011-2022 走看看