-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueuesComparison.java
More file actions
47 lines (33 loc) · 1.33 KB
/
Copy pathQueuesComparison.java
File metadata and controls
47 lines (33 loc) · 1.33 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
import java.util.Random;
public class QueuesComparison {
private static double testQueue(Queue<Integer> q, int opCount) {
long startTime = System.nanoTime();
Random random = new Random();
for (int i = 0; i < opCount; i++) {
q.enqueue(random.nextInt(Integer.MAX_VALUE));
}
for (int i = 0; i < opCount; i++) {
q.dequeue();
}
long endTime = System.nanoTime();
return (endTime - startTime) / 1000000000.0;
}
public static void main(String[] args) {
int opCount = 100000;
ArrayQueue<Integer> arrayQueue = new ArrayQueue<>();
double time1 = testQueue(arrayQueue, opCount); // O(n^2)
System.out.println("ArrayQueue, time1: " + time1 + " S");
LoopQueue<Integer> loopQueue = new LoopQueue<>();
double time2 = testQueue(loopQueue, opCount);
System.out.println("LoopQueue, time2: " + time2 + " S");
LinkedListQueue<Integer> linedListQueue = new LinkedListQueue<>();
double time3 = testQueue(linedListQueue, opCount);
System.out.println("linedListQueue, time3: " + time3 + " S");
/*
Result on my device:
ArrayQueue, time1: 3.58870863 S
LoopQueue, time2: 0.009388672 S
linedListQueue, time3: 0.007742912 S
*/
}
}