Search in sources :

Example 16 with Stopwatch

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

the class Exercise33_DriverSelfOrganizingSearch method doExperiment.

private void doExperiment(Map<Integer, Comparable[]> allInputArrays) {
    StdOut.printf("%10s %25s %25s\n", "Array Size | ", "Self-Organizing Search Time | ", "Default Binary Search Time");
    for (int i = 0; i < allInputArrays.size(); i++) {
        BinarySearchSymbolTable<Integer, Integer> binarySearchSymbolTable = new BinarySearchSymbolTable<>();
        Exercise22_SelfOrganizingSearch selfOrganizingSearch = new Exercise22_SelfOrganizingSearch();
        Exercise22_SelfOrganizingSearch.ArraySTSelfOrganizing<Integer, Integer> arraySTSelfOrganizing = selfOrganizingSearch.new ArraySTSelfOrganizing<>(2);
        Comparable[] array = allInputArrays.get(i);
        double[] probabilityDistribution = new double[array.length];
        for (int p = 0; p < array.length; p++) {
            probabilityDistribution[p] = Math.pow((1.0 / 2.0), p + 1);
        }
        // Self-Organizing Search Symbol Table
        Stopwatch selfOrganizingSearchTimer = new Stopwatch();
        for (Comparable key : array) {
            int randomValue = StdRandom.uniform(0, 2);
            arraySTSelfOrganizing.put((int) key, randomValue);
        }
        for (int search = 0; search < 10 * array.length; search++) {
            double keyProbabilityToSearch = StdRandom.uniform();
            for (int p = 0; p < probabilityDistribution.length; p++) {
                keyProbabilityToSearch -= probabilityDistribution[p];
                if (keyProbabilityToSearch <= 0) {
                    // Search hit
                    arraySTSelfOrganizing.get((int) array[p]);
                    break;
                }
            }
        }
        double selfOrganizingSearchRunningTime = selfOrganizingSearchTimer.elapsedTime();
        // Binary Search Symbol Table
        Stopwatch binarySearchTimer = new Stopwatch();
        for (Comparable key : array) {
            int randomValue = StdRandom.uniform(0, 2);
            binarySearchSymbolTable.put((int) key, randomValue);
        }
        for (int search = 0; search < 10 * array.length; search++) {
            double keyProbabilityToSearch = StdRandom.uniform();
            for (int p = 0; p < probabilityDistribution.length; p++) {
                keyProbabilityToSearch -= probabilityDistribution[p];
                if (keyProbabilityToSearch <= 0) {
                    // Search hit
                    binarySearchSymbolTable.get((int) array[p]);
                    break;
                }
            }
        }
        double binarySearchRunningTime = binarySearchTimer.elapsedTime();
        printResults(array.length, selfOrganizingSearchRunningTime, binarySearchRunningTime);
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 17 with Stopwatch

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

the class Exercise35_PerformanceValidationI method doExperiments.

private void doExperiments(int numberOfExperiments, String[] words) {
    int numberOfWords = 1000;
    int minLength = 4;
    StdOut.printf("%18s %15s %10s %15s\n", "Number of Words | ", "Running Time | ", "Ratio | ", "Complexity (N^X)");
    // Previous time
    String[] currentWords = new String[numberOfWords / 2];
    System.arraycopy(words, 0, currentWords, 0, currentWords.length);
    Stopwatch initialTimer = new Stopwatch();
    frequencyCounter(currentWords, minLength);
    double previousRunningTime = initialTimer.elapsedTime();
    for (int i = 0; i < numberOfExperiments; i++) {
        currentWords = new String[numberOfWords];
        System.arraycopy(words, 0, currentWords, 0, currentWords.length);
        Stopwatch timer = new Stopwatch();
        frequencyCounter(currentWords, minLength);
        double runningTime = timer.elapsedTime();
        double ratio = runningTime / previousRunningTime;
        double lgRatio = Math.log10(ratio) / Math.log10(2);
        printResults(numberOfWords, runningTime, ratio, lgRatio);
        previousRunningTime = runningTime;
        numberOfWords *= 2;
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 18 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(String[] words, int minLength) {
    double totalTimeSpentInPut = 0;
    double totalTimeSpentInGet = 0;
    Stopwatch timer;
    BinarySearchSymbolTable<String, Integer> binarySearchSymbolTable = new BinarySearchSymbolTable<>();
    for (String word : words) {
        if (word.length() < minLength) {
            continue;
        }
        if (!binarySearchSymbolTable.contains(word)) {
            timer = new Stopwatch();
            binarySearchSymbolTable.put(word, 1);
            totalTimeSpentInPut += timer.elapsedTime();
        } else {
            timer = new Stopwatch();
            int currentFrequency = binarySearchSymbolTable.get(word);
            totalTimeSpentInGet += timer.elapsedTime();
            timer = new Stopwatch();
            binarySearchSymbolTable.put(word, currentFrequency + 1);
            totalTimeSpentInPut += timer.elapsedTime();
        }
    }
    String max = "";
    timer = new Stopwatch();
    binarySearchSymbolTable.put(max, 0);
    totalTimeSpentInPut += timer.elapsedTime();
    for (String word : binarySearchSymbolTable.keys()) {
        timer = new Stopwatch();
        if (binarySearchSymbolTable.get(word) > binarySearchSymbolTable.get(max)) {
            totalTimeSpentInGet += timer.elapsedTime();
            max = word;
        }
    }
    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 19 with Stopwatch

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

the class Exercise27_IgnoreSmallSubarrays method doExperiment.

private static void doExperiment(int numberOfExperiments, int initialArraySize, Map<Integer, Comparable[]> allInputArrays) {
    StdOut.printf("%13s %25s %35s\n", "Array Size | ", "QuickSort W/ Cutoff Running Time |", "QuickSort Ignoring Small SubArrays");
    int arraySize = 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);
        // QuickSort with cutoff for small subarrays
        Stopwatch quickSortWithCutoffTimer = new Stopwatch();
        QuickSortWithCutoff.quickSortWithCutoff(originalArray, CUTOFF_SIZE);
        double quickSortWithCutoffRunningTime = quickSortWithCutoffTimer.elapsedTime();
        // QuickSort ignoring small sub arrays
        Stopwatch quickSortIgnoringSmallSubArraysTimer = new Stopwatch();
        quickSortIgnoringSmallSubArrays(array);
        double quickSortIgnoringSmallSubArraysRunningTime = quickSortIgnoringSmallSubArraysTimer.elapsedTime();
        printResults(arraySize, quickSortWithCutoffRunningTime, quickSortIgnoringSmallSubArraysRunningTime);
        arraySize *= 2;
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 20 with Stopwatch

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

the class Exercise30_CornerCases method doExperiment.

private static void doExperiment(Map<Integer, Comparable[]> allInputArrays) {
    StdOut.printf("%13s %25s %23s %30s\n", "Array Size | ", "Type | ", "QuickSort W/ Random Shuffle |", "QuickSort W/O Random Shuffle");
    for (int i = 0; i < allInputArrays.size(); i++) {
        Comparable[] originalArray = allInputArrays.get(i);
        Comparable[] array = new Comparable[originalArray.length];
        System.arraycopy(originalArray, 0, array, 0, originalArray.length);
        // QuickSort with initial random shuffle
        Stopwatch quickSortWithRandomShuffleTimer = new Stopwatch();
        QuickSort.quickSort(originalArray);
        double quickSortWithRandomShuffleRunningTime = quickSortWithRandomShuffleTimer.elapsedTime();
        // QuickSort without initial random shuffle
        Stopwatch quickSortWithoutRandomShuffleTimer = new Stopwatch();
        quickSortWithoutRandomShuffle(array);
        double quickSortWithoutRandomShuffleRunningTime = quickSortWithoutRandomShuffleTimer.elapsedTime();
        String typeOfArray = getTypeOfArray(i);
        printResults(originalArray.length, typeOfArray, quickSortWithRandomShuffleRunningTime, quickSortWithoutRandomShuffleRunningTime);
    }
}
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