java反射机制的性能问题,对比测试!

package com.dasenlin.reflectionconstractor;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 对比反射机制下程序运行的效率
 * @author Administrator
 *
 */
public class Effectiveness {
    /**
     * 正常调动
     */
    public static void test01(){
        User u = new User();
        long startTime = System.currentTimeMillis();
        for(int i=0;i<1000000000L;i++){
            u.getName();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("普通方法调用执行10亿次,耗时"+(endTime-startTime));
    }
    /**
     * 有安全监测机制的调用
     * @throws SecurityException 
     * @throws NoSuchMethodException 
     * @throws InvocationTargetException 
     * @throws IllegalArgumentException 
     * @throws IllegalAccessException 
     */
    @SuppressWarnings("all")
    public static void test02() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
        User u = new User();
        Class  clazz = u.getClass();
        Method m =clazz.getDeclaredMethod("getName",null);
        long startTime = System.currentTimeMillis();
        for(int i=0;i<1000000000L;i++){
            m.invoke(u, null);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("反射机制调用执行10亿次,耗时"+(endTime-startTime));
    }
    /**
     * 跳过安全检查的调用
     * @throws NoSuchMethodException
     * @throws SecurityException
     * @throws InvocationTargetException 
     * @throws IllegalArgumentException 
     * @throws IllegalAccessException 
     */
    @SuppressWarnings("all")
    public static void test03() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
        User u =new User();
        Class calzz =u.getClass();
        Method m =calzz.getDeclaredMethod("getName", null);
        m.setAccessible(true);
        long startTime = System.currentTimeMillis();
        for(int i=0;i<1000000000L;i++){
            m.invoke(u, null);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("反射机制跳过安全检查调用执行10亿次,耗时"+(endTime-startTime));

    }
    /**
     * 测试方法调用
     * @param args
     * 本机测试:普通方法调用执行10亿次,耗时1777ms
     * 反射机制调用执行10亿次,耗时47946ms
     * 反射机制跳过安全检查调用执行10亿次,耗时10482ms
     */
    public static void main(String[] args) {
            test01();
        try {
            test02();
            test03();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值