Deep replication through serialization (Java)

Source: Internet
Author: User
Tags int size object serialization serialization

If you look at the Java 1.1 object serialization example that is described in chapter 10th, you may find that if you undo serialization of an object after serialization, or assemble it, the actual experience is a "cloning" process.
So why not use serialization for deep replication? The following example compares the two methods by calculating the execution time:
 

: Compete.java import java.io.*;

Class Thing1 implements Serializable {} class Thing2 implements Serializable {Thing1 O1 = new Thing1 ();}
    Class Thing3 implements Cloneable {public Object Clone () {object o = null;
    try {o = Super.clone ();
    catch (Clonenotsupportedexception e) {System.out.println ("Thing3 can ' t clone");
  return o;
  } class Thing4 implements cloneable {Thing3 O3 = new Thing3 ();
    Public Object Clone () {Thing4 o = null;
    try {o = (Thing4) super.clone ();
    catch (Clonenotsupportedexception e) {System.out.println ("Thing4 can ' t clone");
    }//Clone the field, Too:o.o3 = (Thing3) o3.clone ();
  return o;
  } public class Compete {static final int SIZE = 5000;
    public static void Main (string[] args) {thing2[] a = new thing2[size];
    for (int i = 0; i < a.length i++) a[i] = new Thing2 ();
    Thing4[] B = new Thing4[size];
    for (int i = 0; i < b.length; i++)  B[i] = new Thing4 ();
      try {Long T1 = System.currenttimemillis ();
      Bytearrayoutputstream buf = new Bytearrayoutputstream ();
      ObjectOutputStream o = new ObjectOutputStream (BUF);
      for (int i = 0; i < a.length i++) O.writeobject (a[i));
            Copies:objectinputstream in = new ObjectInputStream (New Bytearrayinputstream (
      Buf.tobytearray ()));
      Thing2[] C = new Thing2[size];
      for (int i = 0; i < c.length i++) c[i] = (Thing2) in.readobject ();
      Long t2 = System.currenttimemillis ();
      System.out.println ("Duplication via serialization:" + (T2-T1) + "milliseconds");
      Now try cloning:t1 = System.currenttimemillis ();
      Thing4[] D = new Thing4[size];
      for (int i = 0; i < d.length i++) d[i] = (THING4) b[i].clone ();
      T2 = System.currenttimemillis ();
  System.out.println ("Duplication via cloning:" +      (T2-T1) + "milliseconds");
    catch (Exception e) {e.printstacktrace (); }
  }
} ///:~

Where Thing2 and Thing4 contain member objects, there is a need for some deep replication. One interesting place is that although the serializable classes are easy to set up, they do much more work when replicating them. Cloning involves a lot of class setup work, but actual object replication is fairly straightforward. The result is a good illustration of everything. The following are the results of a few runs, respectively:
Do

Duplication via serialization:3400 milliseconds
duplication via cloning:110 milliseconds

via serialization:3410 milliseconds
duplication via cloning:110 milliseconds duplication

via serialization:3520 Milliseconds
duplication via cloning:110 milliseconds

In addition to the huge time difference between serialization and cloning, we also note that serialization technology is not running as well, and that cloning takes the same amount of time every once in a while.

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.