2015-09-07 11:44:36 -06:00
|
|
|
// references/Compete.java
|
2015-12-15 11:47:04 -08:00
|
|
|
// (c)2016 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
|
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
class Thing1 implements Serializable {}
|
|
|
|
class Thing2 implements Serializable {
|
2016-01-25 18:05:55 -08:00
|
|
|
Thing1 t1 = new Thing1();
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class Thing3 implements Cloneable {
|
|
|
|
@Override
|
2016-01-25 18:05:55 -08:00
|
|
|
public Thing3 clone() {
|
2015-06-15 17:47:35 -07:00
|
|
|
try {
|
2016-01-25 18:05:55 -08:00
|
|
|
return (Thing3)super.clone();
|
2015-06-15 17:47:35 -07:00
|
|
|
} catch(CloneNotSupportedException e) {
|
2016-01-25 18:05:55 -08:00
|
|
|
throw new RuntimeException(e);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Thing4 implements Cloneable {
|
2016-01-25 18:05:55 -08:00
|
|
|
private Thing3 t3 = new Thing3();
|
2015-06-15 17:47:35 -07:00
|
|
|
@Override
|
2016-01-25 18:05:55 -08:00
|
|
|
public Thing4 clone() {
|
|
|
|
Thing4 t4 = null;
|
2015-06-15 17:47:35 -07:00
|
|
|
try {
|
2016-01-25 18:05:55 -08:00
|
|
|
t4 = (Thing4)super.clone();
|
2015-06-15 17:47:35 -07:00
|
|
|
} catch(CloneNotSupportedException e) {
|
2016-01-25 18:05:55 -08:00
|
|
|
throw new RuntimeException(e);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
// Clone the field, too:
|
2016-01-25 18:05:55 -08:00
|
|
|
t4.t3 = t3.clone();
|
|
|
|
return t4;
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Compete {
|
|
|
|
public static final int SIZE = 100000;
|
|
|
|
public static void
|
|
|
|
main(String[] args) throws Exception {
|
|
|
|
Thing2[] a = new Thing2[SIZE];
|
2016-01-25 18:05:55 -08:00
|
|
|
for(int i = 0; i < SIZE; i++)
|
2015-06-15 17:47:35 -07:00
|
|
|
a[i] = new Thing2();
|
|
|
|
Thing4[] b = new Thing4[SIZE];
|
2016-01-25 18:05:55 -08:00
|
|
|
for(int i = 0; i < SIZE; i++)
|
2015-06-15 17:47:35 -07:00
|
|
|
b[i] = new Thing4();
|
|
|
|
long t1 = System.currentTimeMillis();
|
2016-01-25 18:05:55 -08:00
|
|
|
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();
|
|
|
|
}
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
long t2 = System.currentTimeMillis();
|
|
|
|
System.out.println(
|
|
|
|
"Duplication via serialization: " +
|
|
|
|
(t2 - t1) + " Milliseconds");
|
2016-01-25 18:05:55 -08:00
|
|
|
|
2015-06-15 17:47:35 -07:00
|
|
|
// Now try cloning:
|
|
|
|
t1 = System.currentTimeMillis();
|
|
|
|
Thing4[] d = new Thing4[SIZE];
|
2016-01-25 18:05:55 -08:00
|
|
|
for(int i = 0; i < SIZE; i++)
|
|
|
|
d[i] = b[i].clone();
|
2015-06-15 17:47:35 -07:00
|
|
|
t2 = System.currentTimeMillis();
|
|
|
|
System.out.println(
|
|
|
|
"Duplication via cloning: " +
|
|
|
|
(t2 - t1) + " Milliseconds");
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2016-07-27 11:12:11 -06:00
|
|
|
Duplication via serialization: 776 Milliseconds
|
|
|
|
Duplication via cloning: 122 Milliseconds
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|