java反射 fast reflect_Java 反射的性能问题

news/2024/7/6 5:35:11

最近在了解反射优化的东西,做了一个 cglib,java 的反射 api 和直接调用的性能比较

public class SampleBean {

public String echo(String name) {

return name;

}

}

测试代码:

FastClass fastClass = FastClass.create(SampleBean.class);

FastMethod fastMethod = fastClass.getMethod(SampleBean.class.getMethod("echo", String.class));

SampleBean myBean = new SampleBean();

long start = System.currentTimeMillis();

for (int i = 0; i < 1000000; i++) {

fastMethod.invoke(myBean, new Object[]{"haha"+i});

//fastMethod.invoke(myBean, new Object[]{"haha"});

}

System.out.println("fastmethod:" + (System.currentTimeMillis() - start));

start = System.currentTimeMillis();

Method m = SampleBean.class.getMethod("echo", String.class);

for (int i = 0; i < 1000000; i++) {

m.invoke(myBean, "haha"+i);

//m.invoke(myBean, "haha");

}

System.out.println("reflect:"+(System.currentTimeMillis() - start));

start = System.currentTimeMillis();

for (int i = 0; i < 1000000; i++) {

myBean.echo("haha"+i);

//myBean.echo("haha");

}

System.out.println("normal:"+(System.currentTimeMillis() - start));

输出:

fastmethod:196

reflect:139

normal:72

上面一段代码如果跑被注释掉的那一行时间会更短:

fastmethod:38

reflect:33

normal:25

个人猜测,百万次调用参数不变,是不是被优化了?

public class SampleBean {

public String echo(String name, int age) {

//改一下,方法稍微复杂点

List names = new ArrayList<>(100);

for (int i = 0; i < 100; i++) {

names.add(name+i);

}

List ages = new ArrayList<>(100);

for (int i = 0; i < 100; i++) {

ages.add(age+i);

}

return name+"--"+age;

}

}

测试代码:

FastClass fastClass = FastClass.create(SampleBean.class);

FastMethod fastMethod = fastClass.getMethod(SampleBean.class.getMethod("echo", String.class, int.class));

SampleBean myBean = new SampleBean();

myBean.setValue("Hello cglib!");

long start = System.currentTimeMillis();

for (int i = 0; i < 1000000; i++) {

fastMethod.invoke(myBean, new Object[]{"haha"+i, 12});

}

System.out.println("fastmethod:" + (System.currentTimeMillis() - start));

start = System.currentTimeMillis();

Method m = SampleBean.class.getMethod("echo", String.class, int.class);

for (int i = 0; i < 1000000; i++) {

m.invoke(myBean, "haha"+i, 12);

}

System.out.println("reflect:" + (System.currentTimeMillis() - start));

start = System.currentTimeMillis();

for (int i = 0; i < 1000000; i++) {

myBean.echo("haha"+i, 12);

}

System.out.println("normal:" + (System.currentTimeMillis() - start));

这段代码输出:

fastmethod:5533

reflect:4779

normal:4691

结果是 cglib 不如 java 自己的反射 api 快?!!! 直接调和反射差的也不多,是我的这段测试有问题吗?请教一下关于反射优化的问题,谢谢。


http://www.niftyadmin.cn/n/3046002.html

相关文章

SQL Server 中的身份认证讲解

SQL Server 中的身份认证讲解TIME:2009-4-22 | READ:228 Microsoft SQL Server™ 可以在两种安全&#xff08;身份验证&#xff09;模式之一下工作&#xff1a; Windows 身份验证模式&#xff08;Windows 身份验证&#xff09; Windows 身份验证模式使用户得以通过 Micros…

瀑布流效果Demo总结(5)之基于jquery+blocksit.min.js的实现

1.综述 最近研究了时下流行的瀑布流展示效果。 当前共计尝试的方法及其优缺点如下&#xff1a; &#xff08;1&#xff09;基于JQuery框架及blocksit.min.js实现的瀑布流不连续&#xff0c;每列中多多少少都会有一些位置出现空白。&#xff08;2&#xff09;基于JQuery框架&…

SQLServer2000身份验证与安全控制

http://netsecurity.51cto.com 2007-12-11 11:25 计算机基础教程网 计算机基础教程网 我要评论(0) SQL Server 身份验证模式 当" SQL Server 2000 在 Windows NT 或 Windows 2000 上运行时&#xff0c;sysadmin 固定服务器角色成员可以指定下面两种身份验证模式之一&…

【LeetCode题意分析解答】43. Multiply Strings

Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 题意分析&#xff1a; 本题是求两个用string表示的非负大数的乘积&#xff0c;乘数可以是任意大小。 解答&…

IE6 IE7 IE8 对 onreadystatechange 事件的扩充

问题描述 onreadystatechange 事件通常用在基于 XMLHttpRequest 对象的 AJAX 应用中&#xff0c;当的该对象的 load state 改变时&#xff0c;会触发此事件。 但在 IE 中 onreadystatechange 事件是其私有实现的&#xff0c;用于数据加载的 IMG、SCRIPT 和 LINK 标记&#xff0…

编写可维护的 Gruntfile.js

load-grunt-tasks 插件 首先介绍下 load-grunt-tasks 这个插件。 我们一般都会把所有用到的插件以及插件的配置写到 Gruntfile.js 里面&#xff0c;对于小项目来说这个文件最终或许不是很大&#xff0c;但是对于大项目、有很多配置或者很多自定义任务的项目来说&#xff0c;最后…

java restful接口ajax_SSM RESTful ajax——开发Java Web

前段时间在参加学院里的一个比赛的时候和朋友一起弄了一个简单的网络论坛项目&#xff0c;使用的技术有ssm、mysql、ajax、jquery、html等。刚开始的时候打算前后端分离开发&#xff0c;由于以前没有经验&#xff0c;所以就摸索着写。项目中大概是前端编写好html&#xff0c;不…

使用 Named Pipes 创建有效的连接字符串

使用 Named Pipes 创建有效的连接字符串 SQL Server 2008 SQL Server 2005 默认 Microsoft SQL Server 实例侦听“named pipes”协议时&#xff0c;除非用户进行了更改&#xff0c;否则将使用 //./pipe/sql/query 作为管道名称。句点指示该计算机是本地计算机&#xff0c;pip…