-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionarySpeedTest.java
More file actions
118 lines (100 loc) · 4.25 KB
/
DictionarySpeedTest.java
File metadata and controls
118 lines (100 loc) · 4.25 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
import java.lang.IllegalArgumentException;
import java.util.HashMap;
import java.util.Random;
import datastructure.dictionary.AVLDictionary;
import datastructure.dictionary.HashTable;
public class DictionarySpeedTest {
public static String randomString(int length) {
String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
Random rand = new Random();
String str = "";
for(int i = 0; i < length; i++)
str = str + chars.charAt(rand.nextInt(chars.length()));
return str;
}
public static void main(String args[]) {
if(args.length != 1) {
System.err.println("Usage: DictionarySpeedTest <int>");
System.exit(0);
}
Integer[] keys = null;
String[] data = null;
try {
int n = Integer.valueOf(args[0]);
if(n <= 0)
throw new IllegalArgumentException("Input number sould be > 0");
Random rand = new Random();
keys = new Integer[n];
data = new String[n];
for(int i = 0; i < n; i++) {
keys[i] = Math.abs(rand.nextInt());
data[i] = randomString(3);
//System.out.println(keys[i] + ":" + data[i]);
}
} catch(Exception e) {
System.out.println(e);
System.exit(1);
}
long start, end;
HashMap<Integer,String> H = new HashMap<>();
HashTable<Integer,String> T = new HashTable<>();
AVLDictionary<Integer,String> A = new AVLDictionary<>();
System.gc();
System.out.println("***** Insert Speed Test *****");
start = System.currentTimeMillis();
for(int i = 0; i < keys.length; i++)
H.put(keys[i],data[i]);
end = System.currentTimeMillis();
System.out.println("Insert with HashMap: " + (end-start)/1000.0 + "s ");
System.gc();
start = System.currentTimeMillis();
for(int i = 0; i < keys.length; i++)
T.insert(keys[i],data[i]);
end = System.currentTimeMillis();
System.out.println("Insert with HashTable: " + (end-start)/1000.0 + "s ");
System.gc();
start = System.currentTimeMillis();
for(int i = 0; i < keys.length; i++)
A.insert(keys[i],data[i]);
end = System.currentTimeMillis();
System.out.println("Insert with AVLDictionary: " + (end-start)/1000.0 + "s ");
System.gc();
System.out.println("\n***** Search Speed Test *****");
start = System.currentTimeMillis();
for(int i = 0; i < keys.length; i++)
H.get(keys[i]);
end = System.currentTimeMillis();
System.out.println("Search with HashMap: " + (end-start)/1000.0 + "s ");
System.gc();
start = System.currentTimeMillis();
for(int i = 0; i < keys.length; i++)
T.search(keys[i]);
end = System.currentTimeMillis();
System.out.println("Search with HashTable: " + (end-start)/1000.0 + "s ");
System.gc();
start = System.currentTimeMillis();
for(int i = 0; i < keys.length; i++)
A.search(keys[i]);
end = System.currentTimeMillis();
System.out.println("Search with AVLDictionary: " + (end-start)/1000.0 + "s ");
System.gc();
System.out.println("\n***** Delete Speed Test *****");
start = System.currentTimeMillis();
for(int i = 0; i < keys.length; i++)
H.remove(keys[i]);
end = System.currentTimeMillis();
System.out.println("Delete with HashMap: " + (end-start)/1000.0 + "s ");
System.gc();
start = System.currentTimeMillis();
for(int i = 0; i < keys.length; i++)
T.delete(keys[i]);
end = System.currentTimeMillis();
System.out.println("Delete with HashTable: " + (end-start)/1000.0 + "s ");
System.gc();
start = System.currentTimeMillis();
for(int i = 0; i < keys.length; i++)
A.delete(keys[i]);
end = System.currentTimeMillis();
System.out.println("Delete with AVLDictionary: " + (end-start)/1000.0 + "s ");
}
}