Search in sources :

Example 11 with Stopwatch

use of edu.princeton.cs.algs4.Stopwatch in project algorithms-sedgewick-wayne by reneargento.

the class Exercise39_ImprovedAccuracyDoubling method timeTrial.

private static double timeTrial(int n) {
    // Time ThreeSum.count() for n random 6-digit ints.
    int max = 1000000;
    int[] values = new int[n];
    for (int i = 0; i < n; i++) {
        values[i] = StdRandom.uniform(-max, max);
    }
    Stopwatch timer = new Stopwatch();
    int count = threeSumCount(values);
    return timer.elapsedTime();
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 12 with Stopwatch

use of edu.princeton.cs.algs4.Stopwatch in project algorithms-sedgewick-wayne by reneargento.

the class Exercise41_ArrayRepresentation method doExperiment.

private void doExperiment() {
    int maxSize = 1000000;
    int[] valuesToInsert = new int[maxSize];
    int[] valuesToSearch = new int[maxSize];
    int[] valuesToDelete = new int[maxSize];
    for (int i = 0; i < maxSize; i++) {
        int randomValue = StdRandom.uniform(maxSize);
        valuesToInsert[i] = randomValue;
    }
    for (int i = 0; i < maxSize; i++) {
        int randomValue = StdRandom.uniform(maxSize);
        valuesToSearch[i] = randomValue;
    }
    for (int i = 0; i < maxSize; i++) {
        int randomValue = StdRandom.uniform(maxSize);
        valuesToDelete[i] = randomValue;
    }
    // Test BST with array representation
    BinarySearchTreeArrayRepresentation<Integer, Integer> binarySearchTreeArrayRepresentation = new BinarySearchTreeArrayRepresentation<>(maxSize);
    Stopwatch timer = new Stopwatch();
    for (int value : valuesToInsert) {
        binarySearchTreeArrayRepresentation.put(value, value);
    }
    double bstArrayInsertTime = timer.elapsedTime();
    timer = new Stopwatch();
    for (int value : valuesToSearch) {
        binarySearchTreeArrayRepresentation.get(value);
    }
    double bstArraySearchTime = timer.elapsedTime();
    timer = new Stopwatch();
    for (int value : valuesToDelete) {
        binarySearchTreeArrayRepresentation.delete(value);
    }
    double bstArrayDeleteTime = timer.elapsedTime();
    // Test BST
    BinarySearchTree<Integer, Integer> binarySearchTree = new BinarySearchTree<>();
    timer = new Stopwatch();
    for (int value : valuesToInsert) {
        binarySearchTree.put(value, value);
    }
    double bstInsertTime = timer.elapsedTime();
    timer = new Stopwatch();
    for (int value : valuesToSearch) {
        binarySearchTree.get(value);
    }
    double bstSearchTime = timer.elapsedTime();
    timer = new Stopwatch();
    for (int value : valuesToDelete) {
        binarySearchTree.delete(value);
    }
    double bstDeleteTime = timer.elapsedTime();
    StdOut.println("\nExperiment results for N = " + maxSize);
    StdOut.println("Array representation BST insert time = " + bstArrayInsertTime);
    StdOut.println("Array representation BST search time = " + bstArraySearchTime);
    StdOut.println("Array representation BST delete time = " + bstArrayDeleteTime);
    StdOut.println("Standard BST insert time = " + bstInsertTime);
    StdOut.println("Standard BST search time = " + bstSearchTime);
    StdOut.println("Standard BST delete time = " + bstDeleteTime);
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 13 with Stopwatch

use of edu.princeton.cs.algs4.Stopwatch in project algorithms-sedgewick-wayne by reneargento.

the class Exercise43_PutGetRatio method frequencyCounter.

private double[] frequencyCounter(int[] values) {
    double totalTimeSpentInPut = 0;
    double totalTimeSpentInGet = 0;
    Stopwatch timer;
    BinarySearchTree<Integer, Integer> binarySearchTree = new BinarySearchTree<>();
    for (Integer value : values) {
        timer = new Stopwatch();
        // contains() uses get() internally
        boolean containsValue = binarySearchTree.contains(value);
        totalTimeSpentInGet += timer.elapsedTime();
        if (!containsValue) {
            timer = new Stopwatch();
            binarySearchTree.put(value, 1);
            totalTimeSpentInPut += timer.elapsedTime();
        } else {
            timer = new Stopwatch();
            int currentFrequency = binarySearchTree.get(value);
            totalTimeSpentInGet += timer.elapsedTime();
            timer = new Stopwatch();
            binarySearchTree.put(value, currentFrequency + 1);
            totalTimeSpentInPut += timer.elapsedTime();
        }
    }
    int max = 0;
    timer = new Stopwatch();
    binarySearchTree.put(max, 0);
    totalTimeSpentInPut += timer.elapsedTime();
    for (Integer value : binarySearchTree.keys()) {
        timer = new Stopwatch();
        if (binarySearchTree.get(value) > binarySearchTree.get(max)) {
            totalTimeSpentInGet += timer.elapsedTime();
            max = value;
        }
    }
    timer = new Stopwatch();
    String maxFrequency = max + " " + binarySearchTree.get(max);
    totalTimeSpentInGet += timer.elapsedTime();
    double ratio = totalTimeSpentInPut / totalTimeSpentInGet;
    return new double[] { totalTimeSpentInPut, totalTimeSpentInGet, ratio };
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 14 with Stopwatch

use of edu.princeton.cs.algs4.Stopwatch in project algorithms-sedgewick-wayne by reneargento.

the class Exercise45_ActualTimings method frequencyCounter.

private String frequencyCounter(SymbolTable<String, Integer> symbolTable, String[] words, int minLength, String title) {
    String xAxisLabel = "calls to get() or put()";
    String yAxisLabel = "running time";
    double maxNumberOfOperations = 45000;
    double maxRunningTime = 3000;
    int originValue = 0;
    VisualAccumulator visualAccumulator = new VisualAccumulator(originValue, maxNumberOfOperations, maxRunningTime, title, xAxisLabel, yAxisLabel);
    double totalRunningTime = 0;
    Stopwatch timer;
    for (String word : words) {
        if (word.length() < minLength) {
            continue;
        }
        if (!symbolTable.contains(word)) {
            timer = new Stopwatch();
            symbolTable.put(word, 1);
            totalRunningTime += timer.elapsedTime() * 1000;
            visualAccumulator.addDataValue(totalRunningTime, false);
        } else {
            timer = new Stopwatch();
            int wordFrequency = symbolTable.get(word);
            totalRunningTime += timer.elapsedTime() * 1000;
            visualAccumulator.addDataValue(totalRunningTime, false);
            timer = new Stopwatch();
            symbolTable.put(word, wordFrequency + 1);
            totalRunningTime += timer.elapsedTime() * 1000;
            visualAccumulator.addDataValue(totalRunningTime, false);
        }
    }
    String max = "";
    timer = new Stopwatch();
    symbolTable.put(max, 0);
    totalRunningTime += timer.elapsedTime() * 1000;
    visualAccumulator.addDataValue(totalRunningTime, false);
    for (String word : symbolTable.keys()) {
        timer = new Stopwatch();
        int wordFrequency = symbolTable.get(word);
        totalRunningTime += timer.elapsedTime() * 1000;
        visualAccumulator.addDataValue(totalRunningTime, false);
        timer = new Stopwatch();
        int maxWordFrequency = symbolTable.get(max);
        totalRunningTime += timer.elapsedTime() * 1000;
        visualAccumulator.addDataValue(totalRunningTime, false);
        if (wordFrequency > maxWordFrequency) {
            max = word;
        }
    }
    timer = new Stopwatch();
    int maxFrequency = symbolTable.get(max);
    totalRunningTime += timer.elapsedTime() * 1000;
    visualAccumulator.addDataValue(totalRunningTime, false);
    visualAccumulator.writeLastComputedValue();
    return max + " " + maxFrequency;
}
Also used : VisualAccumulator(util.VisualAccumulator) Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 15 with Stopwatch

use of edu.princeton.cs.algs4.Stopwatch in project algorithms-sedgewick-wayne by reneargento.

the class Exercise31_PerformanceDriver method doExperiment.

private void doExperiment(Map<Integer, String[]> allInputArrays) {
    double totalRunningTime = 0;
    StdOut.printf("%20s %10s %10s\n", "Number of Experiments | ", "Array Size | ", "AVG Time Taken");
    for (int experiment = 1; experiment <= NUMBER_OF_EXPERIMENTS; experiment++) {
        BinarySearchSymbolTable<String, Integer> binarySearchSymbolTable = new BinarySearchSymbolTable<>();
        String[] array = allInputArrays.get(experiment - 1);
        Stopwatch timer = new Stopwatch();
        for (String key : array) {
            int randomValue = StdRandom.uniform(0, 2);
            binarySearchSymbolTable.put(key, randomValue);
        }
        for (String key : binarySearchSymbolTable.keys()) {
            // Each key is hit an average of ten times and there is about the same number of misses
            for (int search = 0; search < 20; search++) {
                int randomSearchChoice = StdRandom.uniform(0, 2);
                if (randomSearchChoice == 0) {
                    binarySearchSymbolTable.get(SEARCH_MISS_KEY);
                } else {
                    binarySearchSymbolTable.get(key);
                }
            }
        }
        double currentRunningTime = timer.elapsedTime();
        totalRunningTime += currentRunningTime;
        double averageRunningTime = totalRunningTime / experiment;
        printResults(experiment, array.length, averageRunningTime);
    }
}
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