-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
192 lines (161 loc) · 6.56 KB
/
Main.java
File metadata and controls
192 lines (161 loc) · 6.56 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import java.io.*;
import java.util.*;
public class Main {
private static final int[] N = { 1000, 10000, 100000 };
private static final int[] CHAIN_SIZE = { 928, 8329, 83329 };
private static final int[] QUAD_SIZE = { 2003, 20011, 200003 };
public static void main(String[] args) {
System.out.println("Data Structure Performance Comparing Log");
String basePath = "C:/DevTools/Projects/CS3345-a2/";
String[] insertFiles = {
basePath + "iter1_insert_keys.txt",
basePath + "iter2_insert_keys.txt",
basePath + "iter3_insert_keys.txt"
};
String[] searchFiles = {
basePath + "iter1_search_keys.txt",
basePath + "iter2_search_keys.txt",
basePath + "iter3_search_keys.txt"
};
// this warms up JVM
warmUpJVM();
for (int i = 0; i < 3; i++) {
System.out.println("\nDataset: " + (i + 1) + " (" + N[i] + " elements)");
List<Integer> insertKeys, searchKeys;
try {
insertKeys = readKeys(insertFiles[i]);
searchKeys = readKeys(searchFiles[i]);
} catch (IOException e) {
System.err.println("Error reading files: " + e.getMessage());
continue;
}
// AVL tree
System.out.println("\n AVL Tree");
try {
testAVL(insertKeys, searchKeys);
} catch (Exception e) {
System.err.println("AVL Error: " + e.getMessage());
}
// Splay Tree
System.out.println("\n Splay Tree");
try {
testSplay(insertKeys, searchKeys);
} catch (Exception e) {
System.err.println("Splay Error: " + e.getMessage());
}
// Hash Table - chaining
System.out.println("\n Hash Table (chaining)");
try {
testHash_Chaining(insertKeys, searchKeys, CHAIN_SIZE[i]);
} catch (Exception e) {
System.err.println("Hash (Chaining) Error: " + e.getMessage());
}
// Hash Table - quadratic probing
System.out.println("\n Hash Table (quadratic probing)");
try {
testHash_Probing(insertKeys, searchKeys, QUAD_SIZE[i]);
} catch (Exception e) {
System.err.println("Hash (Quadratic) Error: " + e.getMessage());
}
}
}
// JVM warmup method
private static void warmUpJVM() {
AVL<Integer> temp = new AVL<>();
for (int i = 0; i < 1000; i++)
temp.insert(i);
System.out.println("warmup complete, you may proceed");
}
// reading files
private static List<Integer> readKeys(String filename) throws IOException {
List<Integer> keys = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = br.readLine()) != null) {
line = line.trim();
if (!line.isEmpty())
keys.add(Integer.parseInt(line));
}
}
return keys;
}
// measurement method
private static long[] measure(Runnable operation) {
try {
System.gc();
Thread.sleep(50);
} catch (InterruptedException ignored) {
}
Runtime rt = Runtime.getRuntime();
long before = rt.totalMemory() - rt.freeMemory();
long start = System.nanoTime();
operation.run();
long end = System.nanoTime();
long after = rt.totalMemory() - rt.freeMemory();
long elapsed = (end - start) / 1_000_000; // in ms
long used = after - before;
return new long[] { elapsed, used };
}
// AVL Testing
private static void testAVL(List<Integer> insertKeys, List<Integer> searchKeys) {
AVL<Integer> avl = new AVL<>();
long[] insertStats = measure(() -> {
for (Integer key : insertKeys)
avl.insert(key);
});
System.out.printf("Insert: Time: %d ms | Memory: %d bytes%n", insertStats[0], insertStats[1]);
long[] searchStats = measure(() -> {
for (Integer key : searchKeys)
avl.lookup(key);
});
System.out.printf("Search: Time: %d ms | Memory: %d bytes%n", searchStats[0], searchStats[1]);
}
// Splay Testing
private static void testSplay(List<Integer> insertKeys, List<Integer> searchKeys) {
try {
Splay<Integer> splay = new Splay<>();
long[] insertStats = measure(() -> {
for (Integer key : insertKeys) {
splay.insert(key);
}
});
System.out.printf("Insert: Time: %d ms | Memory: %d bytes%n", insertStats[0], insertStats[1]);
long[] searchStats = measure(() -> {
for (Integer key : searchKeys) {
splay.lookup(key);
}
});
System.out.printf("Search: Time: %d ms | Memory: %d bytes%n", searchStats[0], searchStats[1]);
} catch (Exception e) {
System.out.println("Splay Error: " + e);
}
}
// testing Hash - Chaining
private static void testHash_Chaining(List<Integer> insertKeys, List<Integer> searchKeys, int size) {
Hash_Table ht = new Hash_Table(size);
long[] insertStats = measure(() -> {
for (Integer key : insertKeys)
ht.insertChain(key);
});
System.out.printf("Insert: Time: %d ms | Memory: %d bytes%n", insertStats[0], insertStats[1]);
long[] searchStats = measure(() -> {
for (Integer key : searchKeys)
ht.lookupChain(key);
});
System.out.printf("Search: Time: %d ms | Memory: %d bytes%n", searchStats[0], searchStats[1]);
}
// testing Hashing - quadratic probing
private static void testHash_Probing(List<Integer> insertKeys, List<Integer> searchKeys, int size) {
Hash_Table ht = new Hash_Table(size);
long[] insertStats = measure(() -> {
for (Integer key : insertKeys)
ht.insertQuadratic(key);
});
System.out.printf("Insert: Time: %d ms | Memory: %d bytes%n", insertStats[0], insertStats[1]);
long[] searchStats = measure(() -> {
for (Integer key : searchKeys)
ht.lookupQuadratic(key);
});
System.out.printf("Search: Time: %d ms | Memory: %d bytes%n", searchStats[0], searchStats[1]);
}
}