forked from uva-cs/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataStructureComparison.java
158 lines (137 loc) · 5.97 KB
/
DataStructureComparison.java
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// DataStructuresComparison.java
//
// This program will test the relative speeds of the various
// Collection data structres in the Java 1.5 SDK
//
// Copyright (c) 2007 by Aaron Bloomfield.
import java.util.*;
import java.text.*;
public class DataStructuresComparison {
public static void main (String args[]) {
// various counters and such
final int inserts = 50000;
final int searches = 25000;
final int deletes = 10000;
Integer x;
long insertTime, searchTime, deleteTime, startTime, endTime;
Collection collection = null;
// allow for nice printing of the times
NumberFormat style = NumberFormat.getNumberInstance();
style.setMaximumFractionDigits(3);
style.setMinimumFractionDigits(3);
// print out how many of each operation will be performed
System.out.println ("Operations performed:");
System.out.println ("\t" + inserts + "\tinsertions");
System.out.println ("\t" + searches + "\tsearches");
System.out.println ("\t" + deletes + "\tdeletions");
// create the data structure to be time tested
for ( int j = 0; j >= 0; j++ ) {
collection = null;
switch (j) {
case 0:
collection = new Vector();
System.out.print ("Vector: ");
break;
case 1:
collection = new ArrayList();
System.out.print ("ArrayList: ");
break;
case 2:
collection = new LinkedList();
System.out.print ("LinkedList: ");
break;
case 3:
collection = new HashSet();
System.out.print ("HashSet: ");
break;
case 4:
collection = new TreeSet();
System.out.print ("TreeSet: ");
break;
}
// If no more data structures to try, end the for loop
if ( collection == null )
break;
// insert even numbers from 0 to inserts
startTime = System.currentTimeMillis();
for ( int i = 0; i < inserts; i++ ) {
x = new Integer(i*2);
collection.add(x);
}
endTime = System.currentTimeMillis();
insertTime = endTime-startTime;
// search for even and odd numbers from 0 to searches
startTime = System.currentTimeMillis();
for ( int i = 0; i < searches; i++ ) {
x = new Integer(i);
if ( collection.contains(x) ) { }
}
endTime = System.currentTimeMillis();
searchTime = endTime-startTime;
// delete even and odd numbers from 0 to deletes
startTime = System.currentTimeMillis();
for ( int i = 0; i < deletes; i++ ) {
x = new Integer(i);
collection.remove(x);
}
endTime = System.currentTimeMillis();
deleteTime = endTime-startTime;
// record the finish time, compute time taken, print it out
double timeInSec = (insertTime+searchTime+deleteTime)/1000.0;
System.out.println ("\ttook " + (searchTime+insertTime+deleteTime) +
" ms\t" + style.format(timeInSec) + " sec (insert: " +
insertTime + " ms, search: " + searchTime +
" ms, delete: " + deleteTime + " ms)");
}
// Now try doing the same thing on a Vector, but sort the stuff first
Vector vector = new Vector();
System.out.print ("\nVector, sorted:");
// insert even numbers from 0 to inserts
startTime = System.currentTimeMillis();
for ( int i = 0; i < inserts; i++ ) {
x = new Integer(i*2);
vector.add(x);
}
Collections.sort(vector);
endTime = System.currentTimeMillis();
insertTime = endTime-startTime;
// search for even and odd numbers from 0 to searches
startTime = System.currentTimeMillis();
for ( int i = 0; i < searches; i++ ) {
x = new Integer(i);
if ( Collections.binarySearch(vector,x) >= 0 ) { }
}
endTime = System.currentTimeMillis();
searchTime = endTime-startTime;
// delete even and odd numbers from 0 to deletes
int index;
startTime = System.currentTimeMillis();
for ( int i = 0; i < deletes; i++ ) {
x = new Integer(i);
index = Collections.binarySearch(vector,x);
if ( index >= 0 )
vector.remove(index);
}
endTime = System.currentTimeMillis();
deleteTime = endTime-startTime;
// record the finish time, compute time taken, print it out
double timeInSec = (insertTime+searchTime+deleteTime)/1000.0;
System.out.println ("\ttook " + (searchTime+insertTime+deleteTime) +
" ms\t" + style.format(timeInSec) + " sec (insert: " +
insertTime + " ms, search: " + searchTime +
" ms, delete: " + deleteTime + " ms)");
System.exit(0);
}
}
/** Execution run used in the lectures:
Operations performed:
50000 insertions
25000 searches
10000 deletions
Vector: took 17311 ms 17.311 sec (insert: 30 ms, search: 12620 ms, delete: 4661 ms)
ArrayList: took 17281 ms 17.281 sec (insert: 28 ms, search: 12609 ms, delete: 4644 ms)
LinkedList: took 24255 ms 24.255 sec (insert: 54 ms, search: 17934 ms, delete: 6267 ms)
HashSet: took 122 ms 0.122 sec (insert: 103 ms, search: 9 ms, delete: 10 ms)
TreeSet: took 119 ms 0.119 sec (insert: 93 ms, search: 14 ms, delete: 12 ms)
Vector, sorted: took 294 ms 0.294 sec (insert: 36 ms, search: 19 ms, delete: 239 ms)
*/