Search in sources :

Example 56 with Stopwatch

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

the class Exercise37_AutoboxingPerformance method timeTrial.

private static double timeTrial(long trials, boolean useGenericStack) {
    Stopwatch timer = new Stopwatch();
    // Trials only do a push and a pop operation
    for (int i = 0; i < trials; i++) {
        if (!useGenericStack) {
            fixedCapacityInteger.push(i);
            fixedCapacityInteger.pop();
        } else {
            fixedCapacityGeneric.push(i);
            fixedCapacityGeneric.pop();
        }
    }
    return timer.elapsedTime();
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 57 with Stopwatch

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

the class Exercise38_Naive3Sum method timeTrial.

private static double timeTrial(int n, boolean useEvenNaiverImplementation) {
    // 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();
    if (useEvenNaiverImplementation) {
        evenNaiverThreeSumCount(values);
    } else {
        threeSumCount(values);
    }
    return timer.elapsedTime();
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 58 with Stopwatch

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

the class Exercise42_ProblemSizes method timeTrial.

private double timeTrial(int n, int sumMethod) {
    int max = 1000000;
    int[] numbers = new int[n];
    for (int i = 0; i < n; i++) {
        numbers[i] = StdRandom.uniform(-max, max);
    }
    Stopwatch timer = new Stopwatch();
    switch(sumMethod) {
        case 0:
            twoSum(numbers);
            break;
        case 1:
            twoSumFast(numbers);
            break;
        case 2:
            threeSum(numbers);
            break;
        case 3:
            threeSumFast(numbers);
            break;
    }
    return timer.elapsedTime();
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 59 with Stopwatch

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

the class Exercise32_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[] array = allInputArrays.get(i);
            Stopwatch timer = new Stopwatch();
            testInput(array, false);
            double runningTime = timer.elapsedTime();
            printResults(inputType[input], array.length, runningTime);
        }
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 60 with Stopwatch

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

the class Exercise34_ZipfsLaw method doExperiment.

private void doExperiment(Map<Integer, Comparable[]> allInputArrays) {
    StdOut.printf("%10s %25s %25s\n", "Array Size | ", "Move-to-Front Time | ", "Optimal Arrangement 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++) {
            double lnN = Math.log(array.length);
            probabilityDistribution[p] = 1.0 / ((p + 1) * lnN);
        }
        // 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)

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