/
SetVsListBenchmark.java
38 lines (31 loc) · 1.17 KB
/
SetVsListBenchmark.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
package basics;
import java.util.ArrayList;
import java.util.HashSet;
public class SetVsListBenchmark {
public static void main(String[] args) {
long size = (long)1e8;
System.out.println("Element Count : " + size);
long bt = System.nanoTime();
ArrayList<Integer> al = new ArrayList<>();
for (int i = 0; i < size; i++) {
al.add(i);
}
long at = System.nanoTime();
System.out.println("ArrayList Construction: " + (at - bt) / 1000000.0 + "ms");
long bt2 = System.nanoTime();
HashSet<Integer> hs = new HashSet<>();
for (int i = 0; i < size; i++) {
hs.add(i);
}
long at2 = System.nanoTime();
System.out.println("HashSet Construction: " + (at2 - bt2) / 1000000.0 + "ms");
long bt3 = System.nanoTime();
al.contains(size/2);
long at3 = System.nanoTime();
System.out.println("ArrayList contains : " + (at3 - bt3) / 1000000.0 + "ms");
long bt4 = System.nanoTime();
hs.contains(size/2);
long at4 = System.nanoTime();
System.out.println("HashSet contains : " + (at4 - bt4) / 1000000.0 + "ms");
}
}