forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompete.java
More file actions
89 lines (84 loc) · 2.19 KB
/
Compete.java
File metadata and controls
89 lines (84 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// 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 {
@Override
public Thing3 clone() {
try {
return (Thing3)super.clone();
} catch(CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
}
class Thing4 implements Cloneable {
private Thing3 t3 = new Thing3();
@Override
public 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 void
main(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 Milliseconds
Duplication via cloning: 71 Milliseconds
*/