In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly talks about "what are the differences between several frameworks of Bean replication", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the differences between several frameworks of Bean replication?"
Text
As an employee, the most important thing is not to write the code, but to read the code. when reading the code, this blogger found several differences in implementing two different types of Bean, and carefully compared the performance differences of Bean replication in the spirit of research.
The comparison frameworks are Apache's BeanUtils,PropertyUtils,Spring and BeanUtils,Cglib 's BeanCopier.
This is done by creating a new Project in idea that is dedicated to testing the performance of several types of code. The specific code is as follows: a FromBean and a ToBean.
Public class FromBean {private String name; private int age; private String address; private String idno; private double money; public double getMoney () {return money;} public void setMoney (double money) {this.money = money;} public String getName () {return name;} public void setName (String name) {this.name = name } public int getAge () {return age;} public void setAge (int age) {this.age = age;} public String getAddress () {return address;} public void setAddress (String address) {this.address = address;} public String getIdno () {return idno } public void setIdno (String idno) {this.idno = idno;}}
A BenchmarkTest class for testing
Public class BenchmarkTest {private int count; public BenchmarkTest (int count) {this.count = count; System.out.println ("performance testing" + this.count + "=");} public void benchmark (IMethodCallBack m, FromBean frombean) {try {long begin = new java.util.Date () .getTime (); ToBean tobean = null System.out.println (m.getMethodName () + "start testing"); for (int I = 0; I < count; iTest +) {tobean = m.callMethod (frombean);} long end = new java.util.Date (). GetTime (); System.out.println (m.getMethodName () + "time-consuming" + (end-begin)) System.out.println (tobean.getAddress ()); System.out.println (tobean.getAge ()); System.out.println (tobean.getIdno ()); System.out.println (tobean.getMoney ()); System.out.println (tobean.getName ()); System.out.println ("") } catch (Exception e) {e.printStackTrace ();}
Declaration of the interface
Public interface IMethodCallBack {String getMethodName (); ToBean callMethod (FromBean frombean) throws Exception;}
Test classes used
Public class TestMain {/ * * @ param args * / public static void main (String [] args) {FromBean fb = new FromBean (); fb.setAddress ("Datun Road, Chaoyang District, Beijing"); fb.setAge (20); fb.setMoney (30000.111); fb.setIdno ("110330219879208733"); fb.setName ("Test") IMethodCallBack beanutilCB = new IMethodCallBack () {@ Override public String getMethodName () {return "BeanUtil.copyProperties";} @ Override public ToBean callMethod (FromBean frombean) throws Exception {ToBean toBean = new ToBean (); BeanUtils.copyProperties (toBean, frombean); return toBean }}; IMethodCallBack propertyCB = new IMethodCallBack () {@ Override public String getMethodName () {return "PropertyUtils.copyProperties";} @ Override public ToBean callMethod (FromBean frombean) throws Exception {ToBean toBean = new ToBean (); PropertyUtils.copyProperties (toBean, frombean) Return toBean;}}; IMethodCallBack springCB = new IMethodCallBack () {@ Override public String getMethodName () {return "org.springframework.beans.BeanUtils.copyProperties";} @ Override public ToBean callMethod (FromBean frombean) throws Exception {ToBean toBean = new ToBean () Org.springframework.beans.BeanUtils.copyProperties (frombean, toBean); return toBean;}}; IMethodCallBack cglibCB = new IMethodCallBack () {BeanCopier bc = BeanCopier.create (FromBean.class, ToBean.class, false) Override public String getMethodName () {return "BeanCopier.create";} @ Override public ToBean callMethod (FromBean frombean) throws Exception {ToBean toBean = new ToBean (); bc.copy (frombean, toBean, null); return toBean;}} / / when the number is small, test performance BenchmarkTest bt = new BenchmarkTest (10); bt.benchmark (beanutilCB, fb); bt.benchmark (propertyCB, fb); bt.benchmark (springCB, fb); bt.benchmark (cglibCB, fb); / / Test 10,000 performance tests BenchmarkTest bt10000 = new BenchmarkTest (10000); bt10000.benchmark (beanutilCB, fb) Bt10000.benchmark (propertyCB, fb); bt10000.benchmark (springCB, fb); bt10000.benchmark (cglibCB, fb); / worry about the test results BenchmarkTest bt1000R = new BenchmarkTest (10000); bt1000R.benchmark (cglibCB, fb); bt1000R.benchmark (springCB, fb); bt1000R.benchmark (propertyCB, fb); bt1000R.benchmark (beanutilCB, fb);}}
The test results are as follows
It should be noted, however, that Cglib first caches instances when testing, which is one of the reasons for his better performance. If the cache is removed, there will be some differences in performance, but the overall performance is still very good. From the overall performance, the performance of Cglib's BeanCopier is the best, whether it is a large number of 10,000 tests or less than 10 times, almost all approaching zero loss. Spring is in the case of an increase in the number of times, the performance is better, when there is less data, the performance is worse than PropertyUtils. The performance of PropertyUtils is relatively stable, showing a linear growth trend. The BeanUtil of Apache has the worst performance, whether it is a single Copy or a large number of multiple Copy performance is not very good.
At this point, I believe that you have a deeper understanding of "what are the differences between several frameworks of Bean replication?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.