-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions.java
100 lines (94 loc) · 3.77 KB
/
Functions.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
import java.util.*;
class Functions {
static Random randomGenerator = new Random();
static LinkedHashSet<Integer> randomValuesForSet = new LinkedHashSet<>();
public static void loadSet(Collection<Integer> set, int loadSize) {
if (randomValuesForSet.size() < loadSize) {
while (randomValuesForSet.size() < loadSize) {
randomValuesForSet.add(randomGenerator.nextInt(loadSize));
}
}
set.addAll(randomValuesForSet);
}
public static void load(Collection<Integer> collection, int[] randomValues) {
for (int i = 0; i < randomValues.length; i++) {
collection.add(randomValues[i]);
}
}
public static void loadMap(Map<Integer,Integer> collection,int[] randomValues) {
Random randomGenerator = new Random();
for (int i = 0; i < randomValues.length; i++) {
collection.put(i, randomGenerator.nextInt(100000));
}
}
public static void checkAndRecordTime(HashMap<String,Double> runtime, Collection<Integer> collection,String funcName, int value) {
long startTime;
long endTime;
String collectionName = collection.getClass().getSimpleName();
switch (funcName) {
case "add":
startTime = System.nanoTime();
collection.add(value);
endTime = System.nanoTime();
recordTime(runtime, collectionName, endTime - startTime);
break;
case "contains":
startTime = System.nanoTime();
collection.contains(value);
endTime = System.nanoTime();
recordTime(runtime, collectionName, endTime - startTime);
break;
case "remove":
startTime = System.nanoTime();
collection.remove(value);
endTime = System.nanoTime();
recordTime(runtime, collectionName, endTime - startTime);
break;
case "clear":
startTime = System.nanoTime();
collection.clear();
endTime = System.nanoTime();
recordTime(runtime, collectionName, endTime - startTime);
break;
}
}
public static void checkAndRecordTime(HashMap<String,Double> runtime, Map<Integer,Integer> map,String funcName, int value) {
long startTime;
long endTime;
String mapName = map.getClass().getSimpleName();
switch (funcName) {
case "add":
startTime = System.nanoTime();
map.put(value,randomGenerator.nextInt(100000));
endTime = System.nanoTime();
recordTime(runtime, mapName, endTime - startTime);
break;
case "contains":
startTime = System.nanoTime();
map.containsKey(value);
endTime = System.nanoTime();
recordTime(runtime, mapName, endTime - startTime);
break;
case "remove":
startTime = System.nanoTime();
map.remove(value);
endTime = System.nanoTime();
recordTime(runtime, mapName, endTime - startTime);
break;
case "clear":
startTime = System.nanoTime();
map.clear();
endTime = System.nanoTime();
recordTime(runtime, mapName, endTime - startTime);
break;
}
}
public static void recordTime(HashMap<String,Double> map, String name, long time) {
if (map.containsKey(name)) {
double oldTime = map.get(name);
map.put(name,oldTime + (double)time);
} else {
map.put(name,(double)time);
}
}
}