-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleHashSetPerf.java
102 lines (77 loc) · 2.64 KB
/
SimpleHashSetPerf.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
package ch.hslu.AD.SW04.HashTable;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public class SimpleHashSetPerf {
final private static int MAX_PERF_CYCLES = 10;
final private static int MAX_ITEM_SAMPLES = 100000;
final private static int MAX_SET_SIZE = 100;
//final private static int MAX_SET_SIZE = MAX_ITEM_SAMPLES;
private static Item[] getItems(int amount) {
Item[] items = new Item[amount];
for(int i = 0; i < amount; i++) {
items[i] = new Item(i);
}
return items;
}
public static void main(String[] args) {
doPerfMeasurement(new BucketHashSet<Item>(MAX_SET_SIZE), MAX_PERF_CYCLES, MAX_ITEM_SAMPLES);
// NOTE(TF): not possible to use for more than MAX_SET_SIZE item samples because of eventual internal array overflow.
//doPerfMeasurement(new SoundedHashSet<Item>(MAX_SET_SIZE), MAX_PERF_CYCLES, MAX_ITEM_SAMPLES);
doPerfMeasurement(new HashSet<Item>(MAX_SET_SIZE), MAX_PERF_CYCLES, MAX_ITEM_SAMPLES);
}
final public static void doPerfMeasurement(Set<Item> set, int cycles, int samples) {
Item[] items = getItems(samples);
// measure bucket hashset times
long[] startTimes = new long[cycles];
long[] endTimes = new long[cycles];
for(int i = 0; i < cycles; i++) {
set.clear();
startTimes[i] = System.currentTimeMillis();
for(int j = 0; j < samples; j++) {
set.add(items[j]);
}
endTimes[i] = System.currentTimeMillis();
}
System.out.println(String.format("======== Perf for: %s ========", set.getClass().getName()));
// calculate duration and average
long duration = 0;
for(int i = 0; i < cycles; i++) {
System.out.println(String.format("Cycle %d time: %d", i, endTimes[i] - startTimes[i]));
duration += endTimes[i] - startTimes[i];
}
float averageDuration = duration / (float) cycles;
System.out.println(String.format("==> Average cycle time for %s: %f" , set.getClass().getName(), averageDuration));
System.out.println("========");
}
final public static void log(String msg) {
System.out.println(msg);
}
final public static class Item {
private int value;
public Item(int value) {
this.value = value;
}
@Override
public String toString() {
return String.format("Item [%d]", value);
}
@Override
public int hashCode() {
//log("Calculation hashCode for '" + this + "'");
return Objects.hash(value);
}
@Override
public boolean equals(Object other) {
//log("Check equality of '" + this + "' and '" + other + "'");
if(this == other) {
return true;
}
if(!(other instanceof Item)) {
return false;
}
Item o = (Item) other;
return this.value == o.value;
}
}
}