Search in sources :

Example 61 with Stopwatch

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 };
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 62 with Stopwatch

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);
        }
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Aggregations

Stopwatch (edu.princeton.cs.algs4.Stopwatch)62 ArrayList (java.util.ArrayList)5 In (edu.princeton.cs.algs4.In)2 VisualAccumulator (util.VisualAccumulator)2 Exercise21_ComparableTransactions (chapter2.section1.Exercise21_ComparableTransactions)1 RedBlackBST (chapter3.section3.RedBlackBST)1 LinearProbingHashTable (chapter3.section4.LinearProbingHashTable)1