Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. // This program is copyright VUW.
  2. // You are granted permission to use it to construct your answer to a COMP103 assignment.
  3. // You may not distribute it in any other way without permission.
  4.  
  5. /* Code for COMP103 - 2019T2, Assignment 4
  6. * Name:
  7. * Username:
  8. * ID:
  9. */
  10.  
  11. import ecs100.*;
  12. import java.awt.Color;
  13. import java.util.*;
  14.  
  15. /**
  16. * Measures the performance of different ways of doing a priority queue of Items
  17. * Uses an Item class that has nothing in it but a priority (so it takes
  18. * minimal time to construct a new Item).
  19. * The Item constructor doesn't need any arguments
  20. * Remember that small priority values are the highest priority: 1 is higher priority than 10.
  21. * Three different choices to measure:
  22. * Using a built-in PriorityQueue
  23. * Using an ArrayList, with
  24. * - the head of the queue (the next item to poll) head at 0,
  25. * - the tail of the queue (where the offered item is put) at the end
  26. * - sorting the list every time you add an item.
  27. * Using an ArrayList, with
  28. * - the head of the queue at end
  29. * - the tail of the queue at 0
  30. * - sorting the list (in reverse order) every time you add an item.
  31. *
  32. * Each method should have an items parameter, which is a collection of Items
  33. * which should be initially added to the queue (eg new PriorityQueue<Item>(items); or
  34. * new ArrayList<Item>(items))
  35. * It should then repeatedly dequeue an item from the queue, and enqueue a new Item().
  36. * It should do this 100,000 times.
  37. * (the number of times can be changed using the textField)
  38. * When testing that your methods work correctly, you should have debugging statements
  39. * such as UI.println(queue) in the loop to print out the state of the queue.
  40. * You should comment them out before doing the measurement
  41. */
  42.  
  43. public class MeasuringQueues{
  44.  
  45. private int TIMES=100000; //Number of times to repeatedly dequeue and enqueue item
  46. private double PQtotal = 0;
  47. private double ALfront = 0;
  48. private double ALback = 0;
  49. private static final int averageRun = 10;
  50. /** main method */
  51. public static void main(String[] arguments){
  52. MeasuringQueues mq= new MeasuringQueues();
  53. mq.setupGUI();
  54. }
  55.  
  56. /**
  57. * Setup the GUI
  58. */
  59. public void setupGUI(){
  60. UI.addButton("Measure PQ", this::measurePQ);
  61. UI.addButton("Measure AL from front", this::measureAL);
  62. UI.addButton("Measure AL from end", this::measureALfromEnd);
  63. UI.addButton("find average of all", this::measureEverything);
  64. UI.addButton("Quit", UI::quit);
  65. UI.setDivider(1.0);
  66. }
  67.  
  68. /**
  69. * Create a priority queue using a PriorityQueue,
  70. * adding 1,000 items to the queue. (this needs to vary!!)
  71. * Measure the cost of adding and removing an item from the queue.
  72. */
  73. public void measurePQ(){
  74. PriorityQueue<Item> queue = new PriorityQueue<Item>();
  75. long start = System.currentTimeMillis();
  76. for(int i=0; i<TIMES; i++){
  77. queue.poll();
  78. queue.offer(new Item());
  79. }
  80. long stop = System.currentTimeMillis();
  81. //UI.println("==================");
  82. //UI.printf("measuring as Priority Queue: %.7f ms ", 1.0*(stop-start)/TIMES);//average sorting time for each item
  83. PQtotal += 1.0*(stop-start)/TIMES;
  84. //UI.println("\n==================");
  85. }
  86. public void measureAL(){
  87. ArrayList<Item> theALfront = new ArrayList<Item>();
  88. long start = System.currentTimeMillis();
  89. for(int i = 0; i<TIMES; i++){
  90. theALfront.remove(0);
  91. theALfront.add(new Item());
  92. Collections.sort(theALfront);
  93. }
  94. long stop = System.currentTimeMillis();
  95. //UI.println("==================");
  96. //UI.printf("measuring from front: %.7f ms ", 1.0*(stop-start)/TIMES);//average sorting time for each item
  97. ALfront += 1.0*(stop-start)/TIMES;
  98. //UI.println("\n==================");
  99. }
  100. public void measureALfromEnd(){
  101. ArrayList<Item> theALEnd = new ArrayList<Item>();
  102. long start = System.currentTimeMillis();
  103. for(int i = 0; i<TIMES; i++){
  104. theALEnd.remove(theALEnd.size() - 1);
  105. theALEnd.add(0, new Item());// at position 0 add new item
  106. Collections.sort(theALEnd);
  107. Collections.reverse(theALEnd); //has to come second otherwise
  108. }
  109. long stop = System.currentTimeMillis();
  110. //made 2 loops because it needs to be added first then removed
  111.  
  112. //UI.println("==================");
  113. //UI.printf("measuring from end: %.7f ms ", 1.0*(stop-start)/TIMES);//average sorting time for each item
  114. ALback += 1.0*(stop-start)/TIMES;;
  115. //UI.println("\n==================");
  116. }
  117. 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
  118. if(TIMES < 1000){return;}
  119. for(int i = 0; i < averageRun; i++){
  120. measureALfromEnd();
  121. measureAL();
  122. measurePQ();
  123. }
  124. UI.printf("measuring from PriorityQueue average: %.7f ms \n", PQtotal/averageRun);
  125. UI.printf("measuring from front average: %.7f ms \n", ALfront/averageRun);
  126. UI.printf("measuring from end average: %.7f ms \n", ALback/averageRun);
  127. UI.println("times:" + TIMES);
  128. PQtotal = 0;
  129. ALfront = 0;
  130. ALback = 0;
  131. TIMES = TIMES/10;
  132. measureEverything();
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement