Priority Queues/Timing and Performance/Old: Difference between revisions
From charlesreid1
| Line 64: | Line 64: | ||
add_tim.tic(); | add_tim.tic(); | ||
for(int i=0; i<N; i++) { | |||
q.add(key,val); | q.add(key,val); | ||
} | } | ||
| Line 70: | Line 70: | ||
rm_tim.tic(); | rm_tim.tic(); | ||
for(int i=0; i<N; i++) { | |||
q.removeMin(); | q.removeMin(); | ||
} | } | ||
| Line 77: | Line 77: | ||
sb.append( String.format("%d, ",N) ); | sb.append( String.format("%d, ",N) ); | ||
sb.append( String.format("%.3f, ", add_tim.elapsedms()) ); | sb.append( String.format("%.3f, ", add_tim.elapsedms()/ntrials) ); | ||
sb.append( String.format("%.3f ", rm_tim.elapsedms()) ); | sb.append( String.format("%.3f ", rm_tim.elapsedms()/ntrials) ); | ||
sb.append("\n"); | sb.append("\n"); | ||
} | } | ||
Revision as of 17:58, 19 June 2017
Priority Queues
Priority queues are queues that keep items in the queue in a sorted order, so that the minimum (highest priority) item comes out first.
Priority queue timing hypothesis
The hypothesis is that we will see the following behavior for sorted and unsorted implementations of priority queues:
- Unsorted list - add is O(1), min/remove min is O(N)
- Sorted list - add is O(N), min/remove min is O(1)
Priority queue timing class
Below is a basic class for measuring timing and performance of a sorted priority queue. The basic rundown of what the class does is as follows:
- Loop over different values of N, the size of the array. This is the number of add/remove operations being performed in total on one given array.
- Loop over a "large" number of statistical trials. The average access time over all the trials is reported.
Link on git.charlesreid1.com: https://charlesreid1.com:3000/cs/java/src/master/priority-queues/Timing.java
Class contents:
import java.util.LinkedList;
import java.util.Random;
/** Timing class: measure big-O complexity and runtime of data structures.
*
* Compare algorithms, test structures, and verify expected big-O behavior.
*
*/
public class Timing {
// Tests
public static void main(String[] args) {
sorted_timing();
}
/** Time sorted priority queue. */
public static void sorted_timing() {
// This generates CSV files to verify the following information:
// - add method is O(N)
// - remove min method is O(1)
StringBuffer sb = new StringBuffer();
sb.append("N, Walltime Add (ms), Walltime Rm Min (ms)\n");
int ntrials = 200;
Random r = new Random();
// Loop over values of N
for(int N = (int)(5E3); N <= (int)(5E5); N+=2500) {
Tim add_tim = new Tim();
Tim rm_tim = new Tim();
// Trials counter is always k for Kafka
for(int k = 0; k<ntrials; k++) {
// Each trial is a different sequence of random numbers,
// but the sequence matches between tests of different collection types
SortedPriorityQueue<Integer> q = new SortedPriorityQueue<Integer>();
Integer key = new Integer( r.nextInt() );
Integer val = new Integer( r.nextInt() );
add_tim.tic();
for(int i=0; i<N; i++) {
q.add(key,val);
}
add_tim.toc();
rm_tim.tic();
for(int i=0; i<N; i++) {
q.removeMin();
}
rm_tim.toc();
}
sb.append( String.format("%d, ",N) );
sb.append( String.format("%.3f, ", add_tim.elapsedms()/ntrials) );
sb.append( String.format("%.3f ", rm_tim.elapsedms()/ntrials) );
sb.append("\n");
}
System.out.println(sb.toString());
}
}
Priority queue timing results
Sorted priority queue
The results below are for a sorted priority queue. For a sorted priority queue, the minimum is always at the front, and so removal is an O(1) operation. When items are added to the priority queue they are added in order, so add is an O(N) operation. If you squint and look sideways, you can see a barely perceptible linear increase in the cost of add, versus the more flat curve for removal for a sorted list.
Flags
| Stacks and Queues Part of Computer Science Notes
Series on Data Structures
Stacks and Queues: Python StacksQueues/Python · StacksQueues/Python/ArrayStack · StacksQueues/Python/ArrayQueue · StacksQueues/Python/ArrayDeque StacksQueues/Python/LinkedStack
Stacks and Queues: Java StacksQueues/Java · StacksQueues/Java/ArrayStack · StacksQueues/Java/ArrayQueue · StacksQueues/Java/ArrayQueueFS · StacksQueues/Java/ArrayDeque StacksQueues/Java/LinkedStack · StacksQueues/Java/LinkedQueue · StacksQueues/Java/LinkedDeque
Applications Postfix_Expressions#Stacks · StacksQueues/Subsets · StacksQueues/Subsets/Java
|