// references/Compete.java// (c)2017 MindView LLC: see Copyright.txt// We make no guarantees that this code is fit for any purpose.// Visit http://OnJava8.com for more book information.import java.io.*;import onjava.Timer;class Thing1 implements Serializable {}class Thing2 implements Serializable {Thing1 t1 = new Thing1();}class Thing3 implements Cloneable {@Overridepublic Thing3 clone() {try {return (Thing3)super.clone();} catch(CloneNotSupportedException e) {throw new RuntimeException(e);}}}class Thing4 implements Cloneable {private Thing3 t3 = new Thing3();@Overridepublic Thing4 clone() {Thing4 t4 = null;try {t4 = (Thing4)super.clone();} catch(CloneNotSupportedException e) {throw new RuntimeException(e);}// Clone the field, too:t4.t3 = t3.clone();return t4;}}public class Compete {public static final int SIZE = 100000;public static voidmain(String[] args) throws Exception {Thing2[] a = new Thing2[SIZE];for(int i = 0; i < SIZE; i++)a[i] = new Thing2();Thing4[] b = new Thing4[SIZE];for(int i = 0; i < SIZE; i++)b[i] = new Thing4();Timer timer = new Timer();try(ByteArrayOutputStream buf =new ByteArrayOutputStream();ObjectOutputStream oos =new ObjectOutputStream(buf)) {for(Thing2 a1 : a) {oos.writeObject(a1);}// Now get copies:try(ObjectInputStream in =new ObjectInputStream(new ByteArrayInputStream(buf.toByteArray()))) {Thing2[] c = new Thing2[SIZE];for(int i = 0; i < SIZE; i++)c[i] = (Thing2)in.readObject();}}System.out.println("Duplication via serialization: " +timer.duration() + " Milliseconds");// Now try cloning:timer = new Timer();Thing4[] d = new Thing4[SIZE];for(int i = 0; i < SIZE; i++)d[i] = b[i].clone();System.out.println("Duplication via cloning: " +timer.duration() + " Milliseconds");}}/* Output:Duplication via serialization: 516 MillisecondsDuplication via cloning: 71 Milliseconds*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。