Poor memory: 48-java Interceptor-Comparison of built-in dynamic proxies and CGLIB in JDK (3), 48-javacglib

Source: Internet
Author: User

Poor memory: 48-java Interceptor-Comparison of built-in dynamic proxies and CGLIB in JDK (3), 48-javacglib

The dynamic proxy class in Java must implement an interface, and it is said that reflection is not very efficient. Then CGLIB was born.
Using CGLib to implement dynamic proxy is not restricted by the interface that the proxy class must implement. Besides, CGLib uses the ASM bytecode generation framework at the underlying layer to generate the proxy class using the bytecode technology, theoretically, it is more efficient than using Java reflection.
Then let's test how efficient this operation is.
1. Test preparation
Preparation 1: Good memory as bad pen head 46-java Interceptor-a thorough understanding of the concept of dynamic proxy (1) http://blog.csdn.net/ffm83/article/details/43699619
Preparation 2: better memory than bad pen 47-java Interceptor-using CGLib to implement dynamic proxy (2)
Http://blog.csdn.net/ffm83/article/details/43702321

To make the test results easier to observe, disable all the Print outputs.
JDK version: jdk1.6.0 _ 25

2. source code for testing the running efficiency

Package com. tools; import com. CGLib. wangBaoQiang; import com. proxy. actor;/*** use the push mode * JVM parameter:-XX: PrintCompilation * @ author fan fangming */public class BaseRun_Proxy_Test {public static void main (String [] args) throws Exception {int warmUpCycles = 1000000; // Number of pushing times BaseRun_Proxy_Test se = new BaseRun_Proxy_Test (); System. out. println ("the push loop starts... "); se. runTest (warmUpCycles); System. out. println ("Push end"); Thread. sleep (1000); // pause the System. out. println ("entering the formal loop... "); se. runTest (1); se. runTest (100); se. runTest (10000); se. runTest (1000000); se. runTest (20000000); // 2000 million times, the System's daily access volume System. out. println ("Operation completed");} private void runTest (int iterations) {BaseRun_Proxy_Test lot = new BaseRun_Proxy_Test (); // run the JAVA built-in dynamic proxy long startTime = System. nanoTime (); for (int I = 0; I <iterations; I ++) {lot. getJavaProxyLoop ();} long elapsedTime = System. nanoTime (); System. out. println ("running JAVA built-in dynamic Proxy:" + iterations + ", end, time consumed:" + (elapsedTime-startTime); // run CGLIB long cglibStartTime = System. nanoTime (); for (int I = 0; I <iterations; I ++) {lot. getCGLibLoop ();} long cglibElapsedTime = System. nanoTime (); System. out. println ("Run CGLIB dynamic Proxy:" + iterations + ", end, time consumed:" + (cglibElapsedTime-cglibStartTime);} public void getJavaProxyLoop () {// first find the broker com. proxy. actorJingJiRen proxy = new com. proxy. actorJingJiRen (); // obtain the Actor p = proxy from the broker. getProxy (); String retValue = p. sing ("no thief in the world"); String value = p. dance ("Phoenix Legend");} public void getCGLibLoop () {com. CGLib. actorJingJiRen proxy = new com. CGLib. actorJingJiRen (); // obtain the relevant actor (proxy object) WangBaoQiang p = proxy through the broker. getProxy (); String retValue = p. sing ("no thief in the world"); String value = p. dance ("Phoenix Legend ");}}

3. Running result
Push cycle starts...
Run JAVA built-in dynamic Proxy: 1000000, end, time consumed: 2390557348
Run CGLIB dynamic Proxy: 1000000, end, time consumed: 6111703990
Push ended
Enter the formal cycle...
Run JAVA built-in dynamic Proxy: 1, end, time consumed: 35715
Run CGLIB dynamic Proxy: 1, end, time consumed: 39409
Run JAVA built-in dynamic Proxy: 100, end, time consumed: 381782
Run CGLIB dynamic Proxy: 100, end, time consumed: 1162997
Run JAVA built-in dynamic Proxy: 10000, end, time consumed: 23943050
Run CGLIB dynamic Proxy: 10000, end, time consumed: 58153974
Run JAVA built-in dynamic Proxy: 1000000, end, time consumed: 2403822826
Run CGLIB dynamic Proxy: 1000000, end, time consumed: 5804202226
Run JAVA built-in dynamic Proxy: 20000000, end, time consumed: 48913882774
Run CGLIB dynamic Proxy: 20000000, end, time consumed: 118967606438
Operation completed

4. Operational Efficiency table

The efficiency of CGlib dynamic proxy seems to be lower than the built-in efficiency of JAVA. This may be different from what we have heard.

5. Others
Before you place CGLIB in the java built-in proxy method, the data is basically the same as above.

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.