Question: How do I do a timing test with my current code? I need to loop it 20,000 times and with the findMax method for problem
How do I do a timing test with my current code? I need to loop it 20,000 times and with the findMaxmethod for problem sizes 100,000 to 2,000,000 by steps of 100,000. I need similar structure as this previous timing test that I have done:
startTime = System.nanoTime(); while (System.nanoTime() - startTime < 1000000000) { // empty block } startTime = System.nanoTime(); for (int i = 0; i < timesToLoop; i++) randomClass.lookup(1); midpointTime = System.nanoTime(); for (int i = 0; i < timesToLoop; i++) { //empty block } stopTime = System.nanoTime(); double averageTime = ((midpointTime - startTime) - (stopTime - midpointTime)) / (double) timesToLoop; System.out.println(n + "\t" + averageTime); And this is the code that the timing test is for on the methods: findMax(), and Insert().
package assign03; import java.util.Collection; import java.util.Comparator; import java.util.NoSuchElementException; /** * A priority queue that functions to an advance array and is generic. * * @author Kenry Nguyen * @version February 1, 2023 * * @param - the type of elements contained in this priority queue */ public class SimplePriorityQueue implements PriorityQueue{ private T[] list = (T[]) new Object[10]; private int size = 0; private final Comparator super T> cmp; /** * * A constructor for the SimplePriorityQueue that creates a SimplePriorityQueue object * with the given comparator for the type of element. * * @param cmp a generic comparator for the constructor. */ public SimplePriorityQueue(Comparator super T> cmp){ this.cmp = cmp; } /** * For doubling the size of the SimplePriorityQueue by creating a new array that is double * the size of the original. * */ public void doubleBackingArray(){ T[] largerArray = (T[]) new Object[list.length + 5]; for(int i = 0; i < list.length; i++) largerArray[i] = list[i]; list = (T[]) largerArray; } /** * Finds the max in the SimplePriorityQueue which is determined by the comparator that is given * based on the type of elements. * * @return max - a object that is determined by the type given. * @throws NoSuchElementException */ @Override public T findMax() throws NoSuchElementException { T max = null; for (T e : list) { for (int j = 0; j < list.length; j++) { max = list[j]; if(max == null){ throw new NoSuchElementException(); } else if(list[j+1] == null){ return max; } else if (cmp.compare(max, e) > 0) { max = e; } } return max; } return null; } /** * Retrieves the max element before deleting the element. * * @return The maximum element before being deleted by max being replaced by null. * @throws NoSuchElementException */ @Override public T deleteMax() throws NoSuchElementException { T maxToDeleted = (T) findMax(); for (int i = 0; i < list.length; i++){ if(list[i] == maxToDeleted){ list[i] = null; break; } } return maxToDeleted; } /** * * inserts the element into this priority queue. * * @param item - the element to insert */ @Override public void insert(T item){ if (size == list.length){ doubleBackingArray(); } else if(item == null){ return; } int insertIndex = findInsertIndex((T) item, 0, size - 1); for ( int i = size - 1; i >= insertIndex; i--){ list[i + 1] = list[i]; } list[insertIndex] = (T) item; size++; } /** * * This finds a spot where it is suitable to put the index in based * on descending order. * * @param item The item that is to be inserted and compared with the other elements. * @param start an int that is 0 given that it is the start of the array. * @param end an int that is the number of current elements in the array minus one. * @return */ private int findInsertIndex(T item, int start, int end ){ if (start == end){ if(cmp.compare(item, list[start]) > 0){ return start + 1; } else { return start; } } int mid = (start + end) / 2; if(list[mid] == null){ return 0; } else if (cmp.compare(item, list[mid]) > 0){ return findInsertIndex(item, mid + 1, end); } else{ return findInsertIndex(item, start, mid); } } /** * * Inserts a list of elements from the given collection into the simple priority queue. * * @param coll - the collection of elements to insert */ @Override public void insertAll(Collection coll) { T[] newArray = (T[]) coll.toArray(); for (int i = 0; i < newArray.length; i++){ if(size == list.length) doubleBackingArray(); insert(newArray[i]); } } /** * * Checks for the element if the priority queue contains the element. * * @param item - the element to be checked for containment in this priority queue * @return */ @Override public boolean contains(Object item) { if (binarySearch(list, (T) item) == item){ return true; } return false; } /** * * returns the amount of elements inside the priority queue. * * @return a integer that represents the amount of elements in the priority queue. */ @Override public int size() { return size; } /** * * Checks if the priority queue is empty or not. * * @return false if list is not empty, true if list is empty. */ @Override public boolean isEmpty() { for (T t: list){ if(t != null){ return false; } } return true; } /** * * Clears the priority queue by replacing every element with null. * */ @Override public void clear() { for (int i = 0; i < list.length; i++){ if (list[i] != null){ list[i] = null; } } } /** * * Finds an element in the given array from the target element * A faster way to search for an element. * * @param arr * @param target * @return the element that is searched for in the array. */ public T binarySearch(T[] arr, T target){ int start = 0; int end = size - 1; while (start <= end) { int mid = (start + end) / 2; if (list[mid] == null){ return arr[mid]; } int comparison = cmp.compare(target, list[mid]); if (comparison == 0) { return arr[mid]; } else if (comparison < 0) { end = mid - 1; } else start = mid + 1; } return null; } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
