首页 > 作文

详解提高使用Java反射的效率方法

更新时间:2023-04-07 08:57:42 阅读: 评论:0

在我们平时的工作或者面试中,都会经常遇到“反射”这个知识点,通过“反射”我们可以动态的获取到对象的信息以及灵活晨会制度的调用对象方法等,但是在使用的同时又伴随着另一种声音的出现,那就是“反射”很慢,要少用。难道反射真的很慢?那跟我们平时正常创建对象调用方法比慢多少? 估计很多人都没去测试过,只是”道听途说“。下面我们就直接通过一些测试用例来直观的感受一下”反射“。
正文

准备测试对象

下面先定义一个测试的类testur,只有id跟name属性,以及它百家讲坛 红楼梦们的getter/tter方法,另外还有一个自定义的sayhi方法。

public class testur { private integer id; private string name;  public string sayhi(){  return "hi"; } public integer getid() {  return id; } public void tid(integer id) {  this.id = id; } public string getname() {  return name; } public void tname(string name) {  this.name = name; }}

测试创建100万个对象

// 通过普通方式创建testur对象@testpublic void testcommon(){ long start = system.currenttimemillis(); testur ur = null; int i = 0; while(i<1000000){  ++i;  ur = new testur(); } long end = system.currenttimemillis(); system.out.println("普通对象创建耗时:"+(end - start ) + "ms");}//普通对象创建耗时:10ms
// 通过反射方式创建testur对象@testpublic void testreflexnocache() thro系解ws exception { long start = system.currenttimemillis(); testur ur = null; int i = 0; while(i<1000000){  ++i;  ur = (testur) class.forname("reflexdemo.testur").newinstance(); } long end = system.currenttimemillis(); system.out.println("无缓存反射创建对象耗时:"+(end - start ) + "ms");}//无缓存反射创建对象耗时:926ms

在上面这两个测试方法中,笔者各自测了5次,把他们消耗的时间取了一个平均值,在输出结果中可以看到一个是10ms,一个是926ms,在创建100w个对象的情况下,反射居然慢了90倍左右。wtf?差距居然这么大?难道反射真的这么慢?下面笔者换一种反射的姿势,继续测试一下,看看结果如何

// 通过缓存反射方式创建testur对象@testpublic void testreflexwithcache() throws exception { long start = system.currenttimemillis(); testur ur = null; class rurclass = class.forname("refledemo.testur"); int i = 0; while(i<1000000){  ++i;  ur = (testur) rurclass.newinstance(); } long end = system.currenttimemillis(); system.out.println("通过缓存反射创建对象耗时:"+(end - start ) + "ms");}//通过缓存反射创建对象耗时:41ms

其实通过代码我们可以发现,是class.forname这个方法比较耗时,它实际上调用了一个本地方法,通过这个方法来要求jvm查找并加载指定的类。所以我们在项目中使用的时候,可以把class.forname返回的class对象缓存起来,下一次使用的时候直接从缓存里面获取,这样就极大的提高了获取class的效率。同理,在我们获取constructor、method等对象的时候也可以缓存起来使用,避免每次使用时再来耗费时间创建。

测试反射调用方法

@testpublic void testreflexmethod() throws exception { long start = system.currenttimemillis(); class testurclass = class.forname("refledemo.testur"); testur testur = (testur) testurclass.newinstance(); method method = testurclass.getmethod("sayhi"); int i = 0; while(i<100000000){  ++i;  method.invoke(testur); } long end = system.currenttimemillis(); system.out.println("反射调用方法耗时:"+(end - start ) + "ms");}//反射调用方法耗时:330ms
@testpublic void testreflexmethod() throws exception { long start = system.currenttimemillis(); class testurclass = class.forname("refledemo.testur"); testur testur = (testur) testurclass.newinstance(); method method = testurclass.getmethod("sayhi"); int i = 0; while(i<100000000){  ++i;  method.taccessible(true);  method.invoke(testur); } long钱塘湖春行 end = system.currenttimemillis(); system.out.println("taccessible=true 反射调用方法耗时:"+(end - start ) + "ms");}//taccessible=true 反射调用方法耗时:188ms

这里我们反射调用sayhi方法1亿次,在调用了method.taccessible(true)后,发现快了将近一半。查看api可以了解到,jdk在设置获取字段,调用方法的时候会执行安全访问检查,而此类操作会比较耗时,所以通过taccessible(true)的方式可以关闭安全检查,从而提升反射效率。

极致的反射

除了上面的手段,还有没有什么办法可以更极致的使用反射呢?这里介绍一个高性能反射工具包reflectasm。它是通过字节码生成的方式来实现的反射机制,下面是一个跟java反射的性能比较。

结语

最后总结一下,为了更好的使用反射,我们应该在项目启参军要求动的时候将反射所需要的相关配置及数据加载进内存中,在运行阶段都从缓存中取这些元数据进行反射操作。大家也不用惧怕反射,虚拟机在不断的优化,只要我们方法用的对,它并没有”传闻“中的那么慢,当我们对性能有极致追求的时候,可以考虑通过三方包,直接对字节码进行操作。

本文发布于:2023-04-07 08:57:41,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/cfc1f6644dc7ac500e322d6d8c1e4a24.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:详解提高使用Java反射的效率方法.doc

本文 PDF 下载地址:详解提高使用Java反射的效率方法.pdf

标签:反射   对象   方法   缓存
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图