Evidence-based clone

xiaoxiao2021-03-06  112

A few days ago, I chatted in a cloning topic (see http://blog.9cbs.net/rosen/archive/2004/10/09/129948.aspx). Have a friend's clone that I proposed to improve the efficiency deeply suspicion. Today I will tell the specific explanation.

There is now a typical Vo-class Auto (Lightweight):

package com.test; public class Auto implements Cloneable {private String No; private String Addr; public Object clone () throws CloneNotSupportedException {return super.clone ();} public String setNo (String no) {return this.No = no; PUBLIC STRING GETDR (String Addr) {Return this.addr ()}} public string getaddr;}}

The auto instance is then constructed by using clones and BEANs without clones, respectively.

Use cloning bean:

Package com.test; import java.io. *; import java.util. *; import org.dom4j. *; import org.dom4j.io. *; public class myxmlreader {auto auto = new auto (); // Please comparison of different ArrayList al = new ArrayList (); public ArrayList go () {long lasting = System.currentTimeMillis (); try {File f = new File ( "data_100k.xml"); SAXReader reader = new SAXReader (); Document doc = Reader.Read (f); element root = doc.getrootElement (); element foo; for (iTerator i = root.elementiterator ("value"); I.hasNext ();) {foo = (element) i.next (); Auto.ElementText ("no"); auto.detext ("addr"); al.add (auto.clone ()); // compare different}} catch (EXCEPTION E) {E.PrintStackTrace ();} system.out.println ("Run Time:" (System.currentTimeMillis () - Lasting) "Mills"); Return Al;}}

Take eight times run time

Running time: 172 ms

Running time: 93 ms

Running time: 94 ms

Running time: 141 millisecond running time: 125 milliseconds

Running time: 78 ms

Running time: 203 ms

Running time: 63 ms

There is no use of cloning beans:

Package com.test; import java.io. *; import java.util. *; import org.dom4j. *; import org.dom4j.io. *; public class myxmlreader {arraylist al = new arraylist (); public arraylist Go () {Long lasting = system.currenttimemillis (); try {file f = new file ("DATA_100K.XML"); SAXReader Reader = New SaxReader (); Document Doc = Reader.Read (f); Element root = DOC. GetrooteElement (); element foo; for (iTerator i = root.elementiterator ("value"); I.hasNext ();) {foo = (element) i.next (); auto auto = new auto (); // Please compare different auto.setno (Foo.ElementText ("no"); auto.setaddr ("addr")); al.add (auto); // please compare}} catches (Exception E) {E.PrintStackTrace ();} system.out.println ("Run Time:" (System.currentTimeMillis () - Lasting) "Mills"); Return Al;}}

Take eight times run time

Running time: 187 ms

Running time: 93 ms

Running time: 172 ms

Running time: 78 ms

Running time: 204 milliseconds

Running time: 79 ms

Running time: 204 milliseconds

Running time: 78 ms

By comparison, cloning and non-clones are similar to the time to construct AUTO. And slow, let's look at the auto class below. Modify the configuration function of the AUTO class, like this (Heavyweight):

package com.test; import java.io *;. public class Auto implements Cloneable {private String No; private String Addr; private String str; private File f = new File ( "data_10k.xml"); private StringBuffer sb = new StringBuffer (); Public Object Clone () throws clonenotsupportedException {returnus super.clone () // The following method is only to construct a Heavyweight object. Public auto () {Try {BufferedReader Inbuffer = New BufferedReader (New FileReader (f)); while ((str = Inbuffer.Readline ())! = null) {sb.append (str);}} catch (Exception E) {System.out.println (e.tostring ());}} public string setNo (String no) {Return this.no = no;} public string getno ()} public string getno () {Return this.no;} public string setaddr (String Addr ) {Return this.addr = addr;} public string getaddr () {return this.addr;}} Take a look at the test data?

Use Clone Construction Auto Instance: Do not use the clone construction auto instance:

Running time: 188 millisecond running time: 2219 millisecond running time: 2266 millisecond run time: 109 millisecond run time: 2156 millisecond running time: 2093 millisecond Run time: 110 milliseconds: 2266 milliseconds: 2266 milliseconds Running time: 78 milliseconds Run time: 2141 millisecond running time: 157 millisecond run time: 2078 millisecond running time: 78 millisecond running time: 2172 ms

Ok, let's take a look at the Auto class. You can see that just adds the code to read the 10k XML file in its constructor, and the cloning and non-clonal run time has nearly 10 times the gap! Why is this this?

The construction of an object is not just "allocating memory initialized some value domain", it may involve a lot of steps. Therefore, the number and volume of the "to be constructed" will be minimized, and it is wise.

Let us see what happens to create a lightweight class:

1. Assign memory from the HEAP to store instance variables of the Auto class, and a "implementation of specialized data". 2, the instance variables of the Auto class NO and ADDR are initialized to default, and the default is NULL. 3, call the auto class constructor. 4, the AUTO class constructor calls the constructor of the java.lang.object. 5, java.lang.Object constructor returns. 6, the object references "auto" points to the Auto object that is completed in the HEAP. Let us see what happens to create a HeavyWeight class: 1. Assign memory from the HEAP to store instance variables of the Auto class, and a "implementation of special service data". 2, the instance variable of the Auto class NO, ADDR, STR, F, SB, is initialized to default, default is NULL. 3, the constructor of the FILE class is loaded into the 10K XML file to obtain the instance variable f, call the StringBuffer constructor to obtain the instance variable SB, then call the Auto class constructor. (Before constructed, the initial setpoint and initial setting value of all instance variables are executed before the implementation, and then the constructor body is executed. For F and SB, the process starts from step 1.) 4, auto class The constructor of the FileReader class is called in the constructor to load the instance variable F, then call the build function of the BufferedReader class to load the FileReader class instance to obtain the local variable inbuffer. (For the FileReader class, this process is repeated from step 1.) 5, read the data in the Inbuffer, and the instance variable SB is assigned. 6. After the loop is jumped, the instance variable SB returns to the instance variable STR through the method call. 7. The Auto class constructor calls the constructor of the java.lang.Object. 8, java.lang.Object constructor returns. 9. The object references "auto" points to the auto object that is completed in the HEAP. By comparison, it is possible to establish a lot of performance than the performance of the LightWeight object. Step 3, 4 cost is the highest because it has to repeat the whole process for four aggregate objects. Create a cost of the object (especially the Heavyweight object)! Should minimize the number of creations. The less number of creating objects, the faster the code is executed. For the AUTO class (Heavyweight), the object to which the multiplex object references "Auto" is correct. An important way for object multiplexing is cloning. If any class supports the cloning operation, the Cloneable interface must be implemented, which is just an identity interface, which does not implement any way. Any class is announced if it implements Cloneable, the cloning operation is announced. As such code, it is very simple to use clone to improve performance. (Note that the reference should indicate the original author posted this article:! Rosen Jiang and Source: http: //blog.9cbs.net/rosen)

转载请注明原文地址:https://www.9cbs.com/read-98872.html

New Post(0)