2015-09-07 11:44:36 -06:00
|
|
|
// references/Compete.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (c)2021 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.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.io.*;
|
2016-12-17 10:51:25 -08:00
|
|
|
import onjava.Timer;
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
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 {
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override 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();
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public Thing4 clone() {
|
2016-01-25 18:05:55 -08:00
|
|
|
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();
|
2016-12-17 10:51:25 -08:00
|
|
|
Timer timer = new Timer();
|
2016-09-06 12:32:51 -06:00
|
|
|
try(
|
|
|
|
ByteArrayOutputStream buf =
|
|
|
|
new ByteArrayOutputStream();
|
|
|
|
ObjectOutputStream oos =
|
|
|
|
new ObjectOutputStream(buf)
|
|
|
|
) {
|
2016-01-25 18:05:55 -08:00
|
|
|
for(Thing2 a1 : a) {
|
|
|
|
oos.writeObject(a1);
|
|
|
|
}
|
|
|
|
// Now get copies:
|
2016-09-06 12:32:51 -06:00
|
|
|
try(
|
|
|
|
ObjectInputStream in =
|
|
|
|
new ObjectInputStream(
|
|
|
|
new ByteArrayInputStream(
|
|
|
|
buf.toByteArray()))
|
|
|
|
) {
|
2016-01-25 18:05:55 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
System.out.println(
|
|
|
|
"Duplication via serialization: " +
|
2016-12-17 10:51:25 -08:00
|
|
|
timer.duration() + " Milliseconds");
|
2016-01-25 18:05:55 -08:00
|
|
|
|
2015-06-15 17:47:35 -07:00
|
|
|
// Now try cloning:
|
2016-12-17 10:51:25 -08:00
|
|
|
timer = new Timer();
|
2015-06-15 17:47:35 -07:00
|
|
|
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
|
|
|
System.out.println(
|
|
|
|
"Duplication via cloning: " +
|
2016-12-17 10:51:25 -08:00
|
|
|
timer.duration() + " Milliseconds");
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2021-01-31 15:42:31 -07:00
|
|
|
Duplication via serialization: 385 Milliseconds
|
|
|
|
Duplication via cloning: 38 Milliseconds
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|