下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
本文格式为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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 大学三年级《职业生涯规划与就业指导》课程教学设计:基于STARR原则与雇主需求的简历核心能力模型构建
- Thrombin-Inhibitor-2-Standard-生命科学试剂-MCE
- 养老护理员基础知识测试题
- 全科护理服务
- 全科护理环境
- 2025-2030年羽毛球运动用品和器械行业跨境出海战略分析研究报告
- 新形势下童装行业转型升级战略制定与实施分析研究报告
- 2025-2030年业务流程自动化工具行业跨境出海战略分析研究报告
- 高校选拔特殊人才招生规范条例
- 富士达首次覆盖报告:稀缺的射频组件头部玩家防务与商业航天与量子多赛道共振
- 2026年及未来5年内中国高纯丙烯酸行业投资前景及策略咨询研究报告
- 2026年中央驻山西省政法机关直属事业单位工作人员招聘参考题库及参考答案详解【培优】
- 2026年通信工程施工技师试题及答案
- 2026年贵州省算力科技有限责任公司第一批人员招聘20人笔试备考试题及答案详解
- 2026四川凉山州发展(控股)集团有限责任公司所属企业招聘专业技术人员及管理人员9人笔试参考试题及答案详解
- 2026年中国储备粮管理集团夏季招聘(208人)笔试参考试题及答案详解
- 2025年云南省文山州事业单位选调考试综合能力测试综合训练题库及答案
- 2026年(综合知识、综合应用能力测试)四川省机关事业单位考调、选调工作人员测试题及答案
- 梦幻西游交易账号合同
- 广东省深圳市龙岗区2024-2025学年八年级下学期英语期末真题卷(含答案)
- 汽车充电桩操作人员岗前培训指南 (标准版)
评论
0/150
提交评论