-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompare.java
More file actions
83 lines (77 loc) · 2.13 KB
/
Copy pathCompare.java
File metadata and controls
83 lines (77 loc) · 2.13 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
import java.util.Iterator;
import java.util.Random;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentSkipListSet;
public class Compare {
public static void main(String[] args) {
System.out.println("Testing SkipList:");
test(new ConcurrentSkipListSet<Long>(), new Random());
System.out.println("Testing RB Tree:");
test(new TreeSet<Long>(), new Random());
System.out.println("Testing TreeList:");
testTL(new TreeList<Long, Long>(), new Random());
}
public static void testTL(TreeList<Long, Long> s, Random r) {
System.out.println("Add:");
timer();
for (int i = 0; i < 1000000; i++) {
long n = r.nextLong();
s.add(n, n);
}
timer();
System.out.println("Contains:");
timer();
for (int i = 0; i < 1000000; i++)
s.contains(r.nextLong());
timer();
System.out.println("Iterate:");
timer();
Iterator<Long> i = s.rangeIterator((long) r.nextInt(), s.index.lastKey());
while (i.hasNext())
i.next();
timer();
}
public static void test(SortedSet<Long> s, Random r) {
System.out.println("Add:");
timer();
for (int i = 0; i < 1000000; i++)
s.add(r.nextLong());
timer();
System.out.println("Contains:");
timer();
for (int i = 0; i < 1000000; i++)
s.contains(r.nextLong());
timer();
System.out.println("Iterate:");
timer();
for (Long e : s.tailSet((long) r.nextInt()))
;
timer();
}
private static int phase = 0;
private static long startTime, endTime, elapsedTime;
/**
* Timer to calculate the running time
*/
public static void timer() {
if (phase == 0) {
startTime = System.currentTimeMillis();
phase = 1;
} else {
endTime = System.currentTimeMillis();
elapsedTime = endTime - startTime;
System.out.println("Time: " + elapsedTime + " msec.");
memory();
phase = 0;
}
}
/**
* This method determines the memory usage
*/
public static void memory() {
long memAvailable = Runtime.getRuntime().totalMemory();
long memUsed = memAvailable - Runtime.getRuntime().freeMemory();
System.out.println("Memory: " + memUsed / 1000000 + " MB / " + memAvailable / 1000000 + " MB.");
}
}