Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This program is copyright VUW.
- // You are granted permission to use it to construct your answer to a COMP103 assignment.
- // You may not distribute it in any other way without permission.
- /* Code for COMP103 - 2019T2, Assignment 4
- * Name:
- * Username:
- * ID:
- */
- import ecs100.*;
- import java.awt.Color;
- import java.util.*;
- /**
- * Measures the performance of different ways of doing a priority queue of Items
- * Uses an Item class that has nothing in it but a priority (so it takes
- * minimal time to construct a new Item).
- * The Item constructor doesn't need any arguments
- * Remember that small priority values are the highest priority: 1 is higher priority than 10.
- * Three different choices to measure:
- * Using a built-in PriorityQueue
- * Using an ArrayList, with
- * - the head of the queue (the next item to poll) head at 0,
- * - the tail of the queue (where the offered item is put) at the end
- * - sorting the list every time you add an item.
- * Using an ArrayList, with
- * - the head of the queue at end
- * - the tail of the queue at 0
- * - sorting the list (in reverse order) every time you add an item.
- *
- * Each method should have an items parameter, which is a collection of Items
- * which should be initially added to the queue (eg new PriorityQueue<Item>(items); or
- * new ArrayList<Item>(items))
- * It should then repeatedly dequeue an item from the queue, and enqueue a new Item().
- * It should do this 100,000 times.
- * (the number of times can be changed using the textField)
- * When testing that your methods work correctly, you should have debugging statements
- * such as UI.println(queue) in the loop to print out the state of the queue.
- * You should comment them out before doing the measurement
- */
- public class MeasuringQueues{
- private int TIMES=100000; //Number of times to repeatedly dequeue and enqueue item
- private double PQtotal = 0;
- private double ALfront = 0;
- private double ALback = 0;
- private static final int averageRun = 10;
- /** main method */
- public static void main(String[] arguments){
- MeasuringQueues mq= new MeasuringQueues();
- mq.setupGUI();
- }
- /**
- * Setup the GUI
- */
- public void setupGUI(){
- UI.addButton("Measure PQ", this::measurePQ);
- UI.addButton("Measure AL from front", this::measureAL);
- UI.addButton("Measure AL from end", this::measureALfromEnd);
- UI.addButton("find average of all", this::measureEverything);
- UI.addButton("Quit", UI::quit);
- UI.setDivider(1.0);
- }
- /**
- * Create a priority queue using a PriorityQueue,
- * adding 1,000 items to the queue. (this needs to vary!!)
- * Measure the cost of adding and removing an item from the queue.
- */
- public void measurePQ(){
- PriorityQueue<Item> queue = new PriorityQueue<Item>();
- long start = System.currentTimeMillis();
- for(int i=0; i<TIMES; i++){
- queue.poll();
- queue.offer(new Item());
- }
- long stop = System.currentTimeMillis();
- //UI.println("==================");
- //UI.printf("measuring as Priority Queue: %.7f ms ", 1.0*(stop-start)/TIMES);//average sorting time for each item
- PQtotal += 1.0*(stop-start)/TIMES;
- //UI.println("\n==================");
- }
- public void measureAL(){
- ArrayList<Item> theALfront = new ArrayList<Item>();
- long start = System.currentTimeMillis();
- for(int i = 0; i<TIMES; i++){
- theALfront.remove(0);
- theALfront.add(new Item());
- Collections.sort(theALfront);
- }
- long stop = System.currentTimeMillis();
- //UI.println("==================");
- //UI.printf("measuring from front: %.7f ms ", 1.0*(stop-start)/TIMES);//average sorting time for each item
- ALfront += 1.0*(stop-start)/TIMES;
- //UI.println("\n==================");
- }
- public void measureALfromEnd(){
- ArrayList<Item> theALEnd = new ArrayList<Item>();
- long start = System.currentTimeMillis();
- for(int i = 0; i<TIMES; i++){
- theALEnd.remove(theALEnd.size() - 1);
- theALEnd.add(0, new Item());// at position 0 add new item
- Collections.sort(theALEnd);
- Collections.reverse(theALEnd); //has to come second otherwise
- }
- long stop = System.currentTimeMillis();
- //made 2 loops because it needs to be added first then removed
- //UI.println("==================");
- //UI.printf("measuring from end: %.7f ms ", 1.0*(stop-start)/TIMES);//average sorting time for each item
- ALback += 1.0*(stop-start)/TIMES;;
- //UI.println("\n==================");
- }
- public void measureEverything(){//this is needed because running it once or twice is never an accurate estimation of how fast something is, so an average is discovered
- if(TIMES < 1000){return;}
- for(int i = 0; i < averageRun; i++){
- measureALfromEnd();
- measureAL();
- measurePQ();
- }
- UI.printf("measuring from PriorityQueue average: %.7f ms \n", PQtotal/averageRun);
- UI.printf("measuring from front average: %.7f ms \n", ALfront/averageRun);
- UI.printf("measuring from end average: %.7f ms \n", ALback/averageRun);
- UI.println("times:" + TIMES);
- PQtotal = 0;
- ALfront = 0;
- ALback = 0;
- TIMES = TIMES/10;
- measureEverything();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement