zoukankan      html  css  js  c++  java
  • 性能对比

    概述

    比较 普通创建的对象 和 反射创建的对象 ,调用方法哪个更快,相差多少

    比较 反射调用方法,关闭和开启 安全权限检查 的性能

    实例

    /**
     * 通过反射创建对象 对比 普通创建对象 性能?
     */
    public class Demo05 {
    
    //    普通方式
        public static void test(){
            User user = new User();
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < 1000000000; i++) {
                user.getName();
            }
            long endTime = System.currentTimeMillis();
            System.out.println("普通方法,执行10亿次:"+ (endTime - startTime) +"ms");
        }
    //    反射方式
        public static void test1() throws Exception {
            User user = new User();
            Class c1 = user.getClass();
            long startTime = System.currentTimeMillis();
            Method getName = c1.getDeclaredMethod("getName", null);
            for (int i = 0; i < 1000000000; i++) {
                getName.invoke(user, null);
            }
            long endTime = System.currentTimeMillis();
            System.out.println("反射方法,执行10亿次:"+ (endTime - startTime) +"ms");
        }
    //    反射方式 关闭安全权限检查
        public static void test2() throws Exception {
            User user = new User();
            Class c1 = user.getClass();
            long startTime = System.currentTimeMillis();
            Method getName = c1.getDeclaredMethod("getName", null);
            for (int i = 0; i < 1000000000; i++) {
                getName.setAccessible(true);
                getName.invoke(user, null);
            }
            long endTime = System.currentTimeMillis();
            System.out.println("反射方法,关闭安全权限检查,执行10亿次:"+ (endTime - startTime) +"ms");
        }
    
        public static void main(String[] args) throws Exception {
            test();
            test1();
            test2();
        }
    }
    
  • 相关阅读:
    下载flash我的三种方法
    随机变换背景图象(一个可以刷新心情的特效)
    禁止缓存
    [模板]字符串算法
    [学习笔记]有上下界的网络流
    [bzoj2809][Apio2012]dispatching
    [四校联考]Easy Problems
    [学习笔记]tarjan
    [vijos1780][NOIP2012]开车旅行
    记第一次打女队
  • 原文地址:https://www.cnblogs.com/gbhh/p/13768186.html
Copyright © 2011-2022 走看看