Search in sources :

Example 26 with Stopwatch

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

the class Exercise36_PerformanceDriverI method doExperiment.

private static void doExperiment(int numberOfExperiments, Map<Integer, Comparable[]> allInputArrays) {
    StdOut.printf("%13s %12s\n", "Array Size | ", "Average Running Time");
    for (int i = 0; i < numberOfExperiments; i++) {
        Comparable[] array = allInputArrays.get(i);
        PriorityQueue<Double> priorityQueue = new PriorityQueue<>(array.length, PriorityQueue.Orientation.MAX);
        double totalTime = 0;
        for (int trial = 0; trial < 5; trial++) {
            Stopwatch timer = new Stopwatch();
            performanceTest(priorityQueue, array);
            double runningTime = timer.elapsedTime();
            totalTime += runningTime;
        }
        double averageRunningTime = totalTime / 4;
        printResults(array.length, averageRunningTime);
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 27 with Stopwatch

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

the class Exercise37_PerformanceDriverII method insertsAndRemovesIn1Second.

private static int insertsAndRemovesIn1Second(PriorityQueue<Double> priorityQueue, Comparable[] keys) {
    // Fill the priority queue
    for (int i = 0; i < keys.length; i++) {
        priorityQueue.insert((double) keys[i]);
    }
    int numberOfRemoveMaxIn1Second = 0;
    int randomIndex = StdRandom.uniform(keys.length);
    Stopwatch timer = new Stopwatch();
    while (timer.elapsedTime() <= 1) {
        priorityQueue.deleteTop();
        numberOfRemoveMaxIn1Second++;
        priorityQueue.insert((double) keys[randomIndex]);
    }
    return numberOfRemoveMaxIn1Second;
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 28 with Stopwatch

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

the class Exercise39_CostOfConstruction method doExperiment.

private static void doExperiment(Map<Integer, Comparable[]> allInputArrays) {
    StdOut.printf("%13s %12s\n", "Array Size | ", "% of Time Spent on Construction");
    for (int i = 0; i < 3; i++) {
        Comparable[] array = allInputArrays.get(i);
        Stopwatch constructHeapTimer = new Stopwatch();
        constructHeap(array);
        double constructHeapRunningTime = constructHeapTimer.elapsedTime();
        Stopwatch sortdownTimer = new Stopwatch();
        sortdown(array);
        double sortdownRunningTime = sortdownTimer.elapsedTime();
        double totalRunningTime = constructHeapRunningTime + sortdownRunningTime;
        double percentageOfTimeSpentOnConstruction = constructHeapRunningTime / totalRunningTime * 100;
        printResults(array.length, percentageOfTimeSpentOnConstruction);
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 29 with Stopwatch

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

the class Exercise23_DuplicatesRevisitedAgain method countDistinctValues.

public void countDistinctValues(boolean useDedup, boolean useStringSet, boolean generateLongValues, int[] values) {
    int numberOfTrials = 10;
    String method;
    String dataStructure;
    String valuesType;
    if (useDedup) {
        method = "DeDup";
        if (!useStringSet) {
            dataStructure = "HashSet";
        } else {
            dataStructure = "StringSet";
        }
    } else {
        method = "Array index count";
        dataStructure = "Array";
    }
    if (!generateLongValues) {
        valuesType = "Integer";
    } else {
        valuesType = "Long";
    }
    /**
     * T = 10
     * N = 10^3, 10^4, 10^5, 10^6, 10^7, 10^8, 10^9
     * M = N/2, N, 2N
     */
    DistinctCounter distinctCounter = new DistinctCounter();
    for (int n = 0; n < values.length; n++) {
        for (int m = 0; m < 3; m++) {
            int numberOfValues = values[n];
            int maxValue = 0;
            if (m == 0) {
                maxValue = numberOfValues / 2;
            } else if (m == 1) {
                maxValue = numberOfValues;
            } else if (m == 2) {
                maxValue = 2 * numberOfValues;
            }
            Stopwatch stopwatch = new Stopwatch();
            for (int trial = 0; trial < numberOfTrials; trial++) {
                // Count the distinct values, but will not be used in this exercise since we are interested
                // only in the running time
                long distinctValues;
                if (!useDedup) {
                    if (!generateLongValues) {
                        distinctValues = distinctCounter.countDistinctIntegerValuesUsingIndex(numberOfValues, maxValue);
                    } else {
                        distinctValues = distinctCounter.countDistinctLongValuesUsingIndex(numberOfValues, maxValue);
                    }
                } else {
                    if (!generateLongValues) {
                        if (!useStringSet) {
                            distinctValues = distinctCounter.deDupWithIntegerValuesAndHashSet(numberOfValues, maxValue);
                        } else {
                            distinctValues = distinctCounter.deDupWithIntegerValuesAndStringSet(numberOfValues, maxValue);
                        }
                    } else {
                        if (!useStringSet) {
                            distinctValues = distinctCounter.deDupWithLongValuesAndHashSet(numberOfValues, maxValue);
                        } else {
                            distinctValues = distinctCounter.deDupWithLongValuesAndStringSet(numberOfValues, maxValue);
                        }
                    }
                }
            }
            double timeSpent = stopwatch.elapsedTime();
            printResults(method, dataStructure, valuesType, numberOfValues, maxValue, timeSpent);
        }
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 30 with Stopwatch

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

the class Exercise25_Dictionary method doExperiment.

private void doExperiment() {
    LookupCSV lookupCSV = new LookupCSV();
    int[] numberOfQueries = { 100000, 1000000, 10000000, 100000000, 1000000000 };
    String[] filePaths = { LARGE_INPUT_FILE_PATH1, LARGE_INPUT_FILE_PATH2 };
    String[] fileNames = { Constants.SURNAMES_CSV_FILE, Constants.SDSS_CSV_FILE };
    String[] dataStructures = { "Trie", "Ternary Search Trie" };
    StdOut.printf("%21s %19s %20s %10s\n", "Data structure |", "Large input | ", "Number of queries | ", "Time spent");
    for (int trieType = 0; trieType < 2; trieType++) {
        for (int q = 0; q < numberOfQueries.length; q++) {
            for (int f = 0; f < filePaths.length; f++) {
                In in = new In(filePaths[f]);
                String line = in.readLine();
                String[] tokens = line.split(",");
                int randomKeyIndex = StdRandom.uniform(tokens.length);
                int randomValueIndex = StdRandom.uniform(tokens.length);
                TrieInterface<String> trie;
                if (trieType == 0) {
                    trie = new Trie<>();
                } else {
                    trie = new TernarySearchTrie<>();
                }
                Stopwatch stopwatch = new Stopwatch();
                lookupCSV.lookup(trie, filePaths[f], randomKeyIndex, randomValueIndex, numberOfQueries[q]);
                double timeSpent = stopwatch.elapsedTime();
                printResults(dataStructures[trieType], fileNames[f], numberOfQueries[q], timeSpent);
            }
        }
    }
}
Also used : In(edu.princeton.cs.algs4.In) 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