use of edu.princeton.cs.algs4.Stopwatch in project algorithms-sedgewick-wayne by reneargento.
the class Exercise37_PutGetRatio method frequencyCounter.
private double[] frequencyCounter(int[] values) {
double totalTimeSpentInPut = 0;
double totalTimeSpentInGet = 0;
Stopwatch timer;
BinarySearchSymbolTable<Integer, Integer> binarySearchSymbolTable = new BinarySearchSymbolTable<>();
for (Integer value : values) {
timer = new Stopwatch();
// contains() uses get() internally
boolean containsValue = binarySearchSymbolTable.contains(value);
totalTimeSpentInGet += timer.elapsedTime();
if (!containsValue) {
timer = new Stopwatch();
binarySearchSymbolTable.put(value, 1);
totalTimeSpentInPut += timer.elapsedTime();
} else {
timer = new Stopwatch();
int currentFrequency = binarySearchSymbolTable.get(value);
totalTimeSpentInGet += timer.elapsedTime();
timer = new Stopwatch();
binarySearchSymbolTable.put(value, currentFrequency + 1);
totalTimeSpentInPut += timer.elapsedTime();
}
}
int max = 0;
timer = new Stopwatch();
binarySearchSymbolTable.put(max, 0);
totalTimeSpentInPut += timer.elapsedTime();
for (Integer value : binarySearchSymbolTable.keys()) {
timer = new Stopwatch();
if (binarySearchSymbolTable.get(value) > binarySearchSymbolTable.get(max)) {
totalTimeSpentInGet += timer.elapsedTime();
max = value;
}
}
timer = new Stopwatch();
String maxFrequency = max + " " + binarySearchSymbolTable.get(max);
totalTimeSpentInGet += timer.elapsedTime();
double ratio = totalTimeSpentInPut / totalTimeSpentInGet;
return new double[] { totalTimeSpentInPut, totalTimeSpentInGet, ratio };
}
use of edu.princeton.cs.algs4.Stopwatch in project algorithms-sedgewick-wayne by reneargento.
the class Exercise38_ExerciseDriver method doExperiment.
private static void doExperiment(int numberOfExperiments, int initialArraySize) {
StdOut.printf("%28s %13s %12s\n", "Input Type | ", "Array Size | ", "Running Time");
String[] inputType = { ORDERED_INPUT, REVERSE_ORDERED_INPUT, SAME_KEYS_INPUT, KEYS_WITH_ONLY_2_VALUES_INPUT };
for (int input = 0; input < 4; input++) {
Map<Integer, Comparable[]> allInputArrays = generateAllArrays(inputType[input], numberOfExperiments, initialArraySize);
for (int i = 0; i < numberOfExperiments; i++) {
Comparable[] originalArray = allInputArrays.get(i);
Comparable[] array = new Comparable[originalArray.length];
System.arraycopy(originalArray, 0, array, 0, originalArray.length);
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(array.length, PriorityQueue.Orientation.MAX);
Stopwatch timer = new Stopwatch();
testInput(priorityQueue, array);
double runningTime = timer.elapsedTime();
printResults(inputType[input], array.length, runningTime);
}
}
}
Aggregations