下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
本文格式为Word版,下载可任意编辑——Cglib与JDK动态代理的运行性能对比Cglib与JDK动态代理的运行性能比较
都说Cglib创立的动态代理的运行性能比JDK动态代理能高出大约10倍,今日抱着怀疑精神验证了一下,发现状况有所不同,遂贴出试验结果,以供参考和探讨。代码很简单,首先,定义一个Test接口,和一个实现TestImpl。Test接口仅定义一个方法test,对传入的int参数加1后返回。代码如下:
packagemy.test;
publicinterfaceTest{
publicinttest(inti);}
packagemy.test;
publicclassTestImplimplementsTest{publicinttest(inti){returni+1;}}
然后,定义了三种代理的实现:装饰者模式实现的代理(decorator),JDK动态代理(dynamicproxy)和Cglib动态代理(cglibproxy)。代码如下:
packagemy.test;
publicclassDecoratorTestimplementsTest{privateTesttarget;
publicDecoratorTest(Testtarget){this.target=target;}
publicinttest(inti){returntarget.test(i);}}
packagemy.test;
importjava.lang.reflect.InvocationHandler;importjava.lang.reflect.Method;importjava.lang.reflect.Proxy;
publicclassDynamicProxyTestimplementsInvocationHandler{privateTesttarget;
privateDynamicProxyTest(Testtarget){this.target=target;}
publicstaticTestnewProxyInstance(Testtarget){return(Test)Proxy.newProxyInstance(DynamicProxyTest.class.getClassLoader(),newClass[]{Test.class},newDynamicProxyTest(target));
}
publicObjectinvoke(Objectproxy,Methodmethod,Object[]args)
throwsThrowable{
returnmethod.invoke(target,args);}}
packagemy.test;
importjava.lang.reflect.Method;
xy.Enhancer;
xy.MethodInterceptor;xy.MethodProxy;
publicclassCglibProxyTestimplementsMethodInterceptor{
privateCglibProxyTest(){}
publicstaticTestnewProxyInstance(ClasstargetInstanceClazz){Enhancerenhancer=newEnhancer();enhancer.setSuperclass(targetInstanceClazz);enhancer.setCallback(newCglibProxyTest());return(Test)enhancer.create();}
publicObjectintercept(Objectobj,Methodmethod,Object[]args,MethodProxyproxy)throwsThrowable{returnproxy.invokeSuper(obj,args);}}
以TestImpl的调用耗时作为基准,对比通过其它三种代理进行调用的耗时。测试代码如下:
packagemy.test;
importjava.util.LinkedHashMap;importjava.util.Map;
publicclassProxyPerfTester{
publicstaticvoidmain(String[]args){//创立测试对象;
TestnativeTest=newTestImpl();
Testdecorator=newDecoratorTest(nativeTest);
TestdynamicProxy=DynamicProxyTest.newProxyInstance(nativeTest);TestcglibProxy=CglibProxyTest.newProxyInstance(TestImpl.class);
//预热一下;
intpreRunCount=10000;
runWithoutMonitor(nativeTest,preRunCount);runWithoutMonitor(decorator,preRunCount);runWithoutMonitor(cglibProxy,preRunCount);runWithoutMonitor(dynamicProxy,preRunCount);
//执行测试;
Maptests=newLinkedHashMap();tests.put(\\nativeTest);tests.put(\decorator);
tests.put(\\dynamicProxy);tests.put(\\cglibProxy);intrepeatCount=3;
intrunCount=1000000;
runTest(repeatCount,runCount,tests);runCount=50000000;
runTest(repeatCount,runCount,tests);}
privatestaticvoidrunTest(intrepeatCount,intrunCount,Maptests){
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 网络多媒体技术复习 网络多媒体复习
- 教育机构师生互动管理流程手册
- 护理形象礼仪培训方案设计
- 社交媒体医疗信息传播规则
- 循证护理学的证据转化
- 2026年中考体育单招试题及答案
- 护理基本操作技能培训课件
- 2026年小学五年级下册素养提升综合卷含答案
- 2026年小学五年级暑假衔接练习试卷含答案
- 2026年小学五年级上册数学图形拼组练习卷含答案
- 2026重庆巫镇高速路收费员招聘1人建设笔试备考试题及答案解析
- 2026年河北省中考麒麟卷数学试题及答案(三)
- 2024大象版小学三年级科学下册第一单元《磁针与南北》每节课教案汇编(含四个教案)
- 2026特种设备使用管理规则解读
- 解读《特种设备使用管理规则》TSG 08-2026与2017版对比
- 2026年奔驰汽车销售激励管理制度
- 义务教育质量监测四年级科学国测核心素养模拟测试题(附答案)
- 卫生专业技术资格考试中医眼科学(中级335)基础知识复习要点精析(2026年)
- 2022年江苏省扬中市卫生系统护士招聘考试《护理学》试卷及答案
- YS/T 337-2009硫精矿
- 2023年图书资料中级考试题库
评论
0/150
提交评论