-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
41 lines (30 loc) · 1.18 KB
/
Main.java
File metadata and controls
41 lines (30 loc) · 1.18 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
import java.util.Random;
public class Main {
public static void main(String[] args) {
int opCount = 300000;
ArrayQueue<Integer> arrayQueue = new ArrayQueue<>();
double time1 = benchmark(arrayQueue, 0);
System.out.println("Array Queue spend time: " + time1 + " s");
LoopQueue<Integer> loopQueue = new LoopQueue<>();
double time2 = benchmark(loopQueue, opCount);
System.out.println("Loop Queue spend time: " + time2 + " s");
LinkedListQueue<Integer> linkedListQueue = new LinkedListQueue<>();
double time3 = benchmark(linkedListQueue, opCount );
System.out.println("Linked Queue spend time: " + time3 + " s");
}
private static double benchmark(Queue<Integer> queue, int opCount)
{
long startTime = System.nanoTime();
Random random = new Random();
// enqueue
for(int i = 0; i < opCount; i ++) {
queue.enqueue(random.nextInt(Integer.MAX_VALUE));
}
// dequeue
for(int i = 0; i < opCount; i ++) {
queue.dequeue();
}
long endTime = System.nanoTime();
return (endTime - startTime) / 1000000000.0;
}
}