- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleSetPerformanceAnalyzer.java
More file actions
Latest commit
132 lines (118 loc) · 4.39 KB
/
Copy pathSimpleSetPerformanceAnalyzer.java
File metadata and controls
132 lines (118 loc) · 4.39 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
importjava.util.HashSet;
importjava.util.LinkedList;
importjava.util.TreeSet;
/**
* this class analyze the performance of various implementations of simple set.
* @author avishavtal
*/
publicclassSimpleSetPerformanceAnalyzer {
/* operations symbols */
privatestaticfinalintADD = 0;
privatestaticfinalintCONTAINS = 3;
/* data structures symbols*/
privatestaticfinalintOPEN_HASH_SET = 3;
privatestaticfinalintCLOSED_HASH_SET = 2;
privatestaticfinalintTREE_SET = 1;
privatestaticfinalintHASH_SET = 0;
privatestaticfinalintLINKED_LIST = 4;
// before each measurement this sets will do the operation for warm up.
privatestaticSimpleSet[] warmUpSets = newSimpleSet[5];
//strings will do the operations on this strings for warm up.
privatestaticString[] warmUpArray;
/* arrays of data for measurement*/
privatestaticfinalString[] data1 = Ex3Utils.file2array("data1.txt");
privatestaticfinalString[] data2 = Ex3Utils.file2array("data2.txt");
// implementations of simple set to analyze their performance.
privatestaticSimpleSet[] sets = newSimpleSet[5];
// initialize the sets.
privatestaticvoidsetSets(SimpleSet[] simpleSets){
simpleSets[OPEN_HASH_SET] = newOpenHashSet();
simpleSets[CLOSED_HASH_SET] = newClosedHashSet();
simpleSets[TREE_SET] = newCollectionFacadeSet(newTreeSet<>());
simpleSets[HASH_SET] = newCollectionFacadeSet(newHashSet<>());
simpleSets[LINKED_LIST] = newCollectionFacadeSet(newLinkedList<>());
}
// initialize the warm up array
privatestaticvoidsetWarmUpArray(){
warmUpArray = newString[data1.length];
for (inti = 0; i < data1.length; i++){
if (i % 2 == 0){
warmUpArray[i] = data2[i];
} else {
warmUpArray[i] = data1[i];
}
}
}
// measure the time it takes to add the given data to each set.
privatestaticvoidmeasureAdd(String[] data){
System.out.println("add results:");
System.out.println();
for (inti =0 ; i < 5; i++){
warmUp(i, ADD);
longbefore = System.nanoTime();
for (Stringstring: data){
sets[i].add(string);
}
longresult = (System.nanoTime() - before)/1000000;
Stringtype = getType(i);
System.out.println(type+": "+result);
}
System.out.println();
}
// measure the time it takes to search the given word in each set.
privatestaticvoidmeasureContains(Stringword){
System.out.println("contains "+word+" results:");
for (inti = 0; i < 5; i++){
warmUp(i, CONTAINS);
intiterations = 0;
longbefore = System.nanoTime();
while ((System.nanoTime()-before)/1000000000 < 5){
iterations++;
sets[i].contains(word);
}
longresult = (System.nanoTime()-before)/iterations;
System.out.println(getType(i)+": "+result);
}
System.out.println();
}
// do the warm up before the measurement.
privatestaticvoidwarmUp(inti, intoperation) {
if (operation == ADD){
for (Stringstring: warmUpArray){
warmUpSets[i].add(string);
}
} else {
for (Stringstring: warmUpArray){
warmUpSets[i].contains(string);
}
}
}
privatestaticStringgetType(inti) {
switch (i){
caseOPEN_HASH_SET:
return"OpenHashSet";
caseCLOSED_HASH_SET:
return"ClosedHashSet";
caseTREE_SET:
return"TreeSet";
caseHASH_SET:
return"HashSet";
caseLINKED_LIST:
return"LinkedList";
}
returnnull;
}
publicstaticvoidmain(String[] args){
setSets(sets);
setSets(warmUpSets);
setWarmUpArray();
measureAdd(data1);
measureContains("hi");
measureContains("-13170890158");
setSets(sets);
setSets(warmUpSets);
measureAdd(data2);
measureContains("23");
measureContains("hi");
}
}