A good memory is better than a bad writer. 48-java Interceptor-jdk with dynamic agent and cglib efficiency comparison (3)

Source: Internet
Author: User

The class of the dynamic proxy that comes with Java must implement an interface, and it is said that the use of reflection is not very efficient. So the Cglib was born.
The use of Cglib to implement dynamic proxy, completely non-agent class must implement the interface constraints, and cglib based on ASM bytecode generation framework, using bytecode technology to generate proxy classes, in theory, than using Java reflection efficiency is higher.
So what's the efficiency of this run, when we test it?
1. Preparation of the test
Preparation 1: Good memory is better than bad writing 46-java interceptor-thoroughly understand the concept of dynamic agents (1) http://blog.csdn.net/ffm83/article/details/43699619
Preparation 2: A good memory is better than a bad pen 47-java interceptor-Dynamic Proxy with Cglib (2)
http://blog.csdn.net/ffm83/article/details/43702321

For easy observation of the test results, turn off all printouts.
JDK version: jdk1.6.0_25

2. The source code of test run efficiency

 PackageCom.tools;ImportCom. Cglib.wangbaoqiang;ImportCom.proxy.Actor;/** * Using warm-up mode * JVM parameters:-xx:printcompilation * @author Fan Fangming */ Public  class baserun_proxy_test {     Public Static void Main(string[] args)throwsexception{intWarmupcycles =1000000;//Preheat timesBaserun_proxy_test SE =NewBaserun_proxy_test (); System.out.println ("Warm-up cycle begins ...");        Se.runtest (Warmupcycles); System.out.println ("Warm-up End"); Thread.Sleep ( +);//Leave system pausedSystem.out.println ("into the formal cycle ..."); Se.runtest (1); Se.runtest ( -); Se.runtest (10000); Se.runtest (1000000); Se.runtest (20000000);//20 billion times, our system visits per daySystem.out.println ("Completion of the formal operation"); }Private void runtest(intIterations) {baserun_proxy_test lot =NewBaserun_proxy_test ();//Run Java Self-brought dynamic agent        LongStartTime = System.nanotime (); for(inti =0;  I < iterations;        i + +) {lot.getjavaproxyloop (); }LongElapsedTime = System.nanotime (); System.out.println ("Running Java comes with dynamic proxy:"+ iterations +", end, time-consuming:"+ (elapsedtime-starttime));//Run Cglib        LongCglibstarttime = System.nanotime (); for(inti =0;  I < iterations;        i + +) {lot.getcglibloop (); }LongCglibelapsedtime = System.nanotime (); System.out.println ("Run Cglib Dynamic agent:"+ iterations +", end, time-consuming:"+ (cglibelapsedtime-cglibstarttime)); } Public void Getjavaproxyloop(){//Find brokers FirstCom.proxy.ActorJingJiRen proxy =NewCom.proxy.ActorJingJiRen ();//Get related actors by broker (Agent object)Actor p = proxy.getproxy (); String RetValue = p.sing ("No Thief in the world."); String value = P.dance ("Phoenix Legend"); } Public void Getcglibloop() {com. Cglib.actorjingjiren proxy =NewCom. Cglib.actorjingjiren ();//Get related actors by broker (Agent object)Wangbaoqiang p = proxy.getproxy (); String RetValue = p.sing ("No Thief in the world."); String value = P.dance ("Phoenix Legend"); }}

3. Operation Result
Warm-up cycle begins ...
Run Java self-brought dynamic proxy: 1000000, end, time consuming: 2390557348
Run Cglib dynamic agent: 1000000, end, time consuming: 6111703990
Preheating End
Into the formal cycle ...
Run Java self-brought dynamic proxy: 1, end, time consuming: 35715
Run Cglib dynamic agent: 1, end, time consuming: 39409
Run Java self-brought dynamic proxy: 100, end, time consuming: 381782
Run Cglib dynamic agent: 100, end, time consuming: 1162997
Run Java self-brought dynamic proxy: 10000, end, time consuming: 23943050
Run Cglib dynamic agent: 10000, end, time consuming: 58153974
Run Java self-brought dynamic proxy: 1000000, end, time consuming: 2403822826
Run Cglib dynamic agent: 1000000, end, time consuming: 5804202226
Run Java self-brought dynamic proxy: 20000000, end, time consuming: 48913882774
Run Cglib dynamic agent: 20000000, end, time consuming: 118967606438
Formal operation completed

4. Table of Operational efficiency

The efficiency of Cglib's dynamic agents seems to be less efficient than Java's own. This may not be the same as the situation we have heard.

5. Other
Before putting Cglib in Java's own proxy method, the data situation is basically the same as above.

A good memory is better than a bad writer. 48-java Interceptor-jdk with dynamic agent and cglib efficiency comparison (3)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.