Java300StudyNote(1)-反射机制-提高反射效率

反射机制的性能问题

使用反射机制调用类或调用类的方法会大大的降低程序的执行效率。一下代码测试的是在不同情况下执行同个类方法一亿次所需时间,结果是

类型执行次数所耗时间
普通方法一亿次4ms
反射一亿次217ms
反射+跳过安全检查一亿次149ms

得出的结论就是在调用反射的时候设置setAccessible(true)可以大大地提高程勋执行效率,原因在于在使用反射时,会进行安全性检查,大大降低了程序的执行效率,而setAccessible(true)就是跳过安全检查以此来提高程序的执行效率,在实际开发中可根据实际需要进行相应的取舍。

以下是测试性能程序和运行结果截图

user.java

public class User {

    public void sayHello(String name) {

    }

}

ReflectionPerformanceTest.java

package com.maple.reflect.demo03;

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

import org.junit.Test;

/**
 * 测试反射的执行效率
 * 
 * @author MapleSky
 *
 */
public class ReflectionPerformanceTest {

    private static final String PATH 
            = "com.maple.reflect.demo03.User";

    /**
     * 使用普通调用sayHello方法一亿次
     */
    public static void test01() {
        User u = new User();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 100_000_000; i++) {
            u.sayHello("你好");
        }
        long end = System.currentTimeMillis();
        System.out.println("普通方法调用sayHello方法一亿次所需时间" 
                + (end - start) + "ms");
    }

    /**
     * 使用反射调用sayHello 但不跳过安全检查
     */
    public static void test02() {
        try {
            Class<?> clazz = Class.forName(PATH);
            long start = System.currentTimeMillis();
            Method sayHello = clazz.getDeclaredMethod("sayHello", String.class);
            User u = new User();
            for (int i = 0; i < 100_000_000; i++) {
                sayHello.invoke(u, "你好");
            }
            long end = System.currentTimeMillis();
            System.out.println("反射调用sayHello方法一亿次不跳过安全检查所需时间" 
                    + (end - start) + "ms");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } 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();
        }
    }

    /**
     * 使用反射调用sayHello 但不跳过安全检查
     */
    public static void test03() {
        try {
            Class<?> clazz = Class.forName(PATH);
            long start = System.currentTimeMillis();
            Method sayHello = clazz.getDeclaredMethod("sayHello", String.class);
            User u = new User();
            sayHello.setAccessible(true);// 跳过安全检查
            for (int i = 0; i < 100_000_000; i++) {
                sayHello.invoke(u, "你好");
            }
            long end = System.currentTimeMillis();
            System.out.println("反射调用sayHello方法一亿次——跳过安全检查所需时间"
                    + (end - start) + "ms");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } 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();
        }
    }

    @Test
    public void app() {
        test01();
        test02();
        test03();
    }

}

运行结果
RhymeChiang

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值