Search in sources :

Example 1 with Stopwatch

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

the class Exercise23_Improvements method testCutoffImprovement.

private static void testCutoffImprovement(int numberOfExperiments, int initialArraySize, Map<Integer, Comparable[]> allInputArrays) {
    StdOut.printf("%10s %13s %12s\n", "Cutoff | ", "Array Size | ", "Running Time");
    for (int cutoff = 5; cutoff <= 30; cutoff += 5) {
        int arraySize = initialArraySize;
        Exercise23_Improvements1_Cutoff improvements1 = new Exercise23_Improvements1_Cutoff(cutoff);
        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);
            Stopwatch timer = new Stopwatch();
            improvements1.mergeSort(array);
            double runningTime = timer.elapsedTime();
            printCutoffResults(cutoff, arraySize, runningTime);
            arraySize *= 2;
        }
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 2 with Stopwatch

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

the class Exercise23_Improvements method doExperiment.

private static void doExperiment(int numberOfExperiments, int initialArraySize, Map<Integer, Comparable[]> allInputArrays, TEST_TYPE testType) {
    StdOut.printf("%13s %12s\n", "Array Size | ", "Running Time");
    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);
        Stopwatch timer = new Stopwatch();
        switch(testType) {
            case BASELINE_MERGESORT:
                TopDownMergeSort.mergeSort(array);
                break;
            case IMPROVEMENT_SKIP_SORTED:
                Exercise23_Improvements2_TestSorted.mergeSort(array);
                break;
            case IMPROVEMENT_AVOID_COPY:
                Exercise23_Improvements3_AvoidCopy.mergeSort(array);
                break;
            case FASTER_MERGE:
                Exercise10_FasterMerge.topDownMergeSort(array);
                break;
        }
        double runningTime = timer.elapsedTime();
        printResults(arraySize, runningTime);
        arraySize *= 2;
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 3 with Stopwatch

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

the class Exercise24_SortTestImprovement method doExperiment.

private void doExperiment(int numberOfExperiments, int initialArraySize, Map<Integer, Comparable[]> allInputArrays, TEST_TYPE testType) {
    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);
        numberOfTimesSortTestSucceeded = 0;
        Stopwatch timer = new Stopwatch();
        switch(testType) {
            case BASELINE_MERGESORT:
                TopDownMergeSort.mergeSort(array);
                break;
            case SORT_TEST:
                mergeSortWithSortTest(array);
                break;
        }
        double runningTime = timer.elapsedTime();
        if (testType == TEST_TYPE.BASELINE_MERGESORT) {
            printResults(arraySize, runningTime);
        } else if (testType == TEST_TYPE.SORT_TEST) {
            double avgTimesTestSucceeded = ((double) numberOfTimesSortTestSucceeded) / ((double) arraySize) * 100;
            printSortTestResults(arraySize, runningTime, avgTimesTestSucceeded);
        }
        arraySize *= 2;
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 4 with Stopwatch

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

the class Exercise22_DoublingTestErdosRenyi method doExperiments.

private void doExperiments(int numberOfExperiments, int unionFindType) {
    List<Experiment> experiments = new ArrayList<>();
    int numberOfSites = 512;
    // Previous time
    UF initialUnionFind = generateUnionFind(numberOfSites / 2, unionFindType);
    Stopwatch initialTimer = new Stopwatch();
    erdosRenyi(numberOfSites / 2, initialUnionFind);
    double previousRunningTime = initialTimer.elapsedTime();
    for (int i = 0; i < numberOfExperiments; i++) {
        UF unionFind = generateUnionFind(numberOfSites, unionFindType);
        Stopwatch timer = new Stopwatch();
        int pairsGenerated = erdosRenyi(numberOfSites, unionFind);
        double runningTime = timer.elapsedTime();
        Experiment experiment = new Experiment(numberOfSites, pairsGenerated, runningTime / previousRunningTime);
        experiments.add(experiment);
        previousRunningTime = runningTime;
        numberOfSites *= 2;
    }
    printResults(experiments);
}
Also used : ArrayList(java.util.ArrayList) Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 5 with Stopwatch

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

the class Exercise24_FastAlgorithmsErdosRenyi method doExperiments.

private void doExperiments(int numberOfExperiments) {
    List<Exercise24_FastAlgorithmsErdosRenyi.Experiment> experiments = new ArrayList<>();
    int numberOfSites = 512;
    for (int i = 0; i < numberOfExperiments; i++) {
        // Weighted QuickUnion
        UF weightedQuickUnion = new WeightedQuickUnion(numberOfSites);
        Stopwatch timer = new Stopwatch();
        List<Exercise18_RandomGridGenerator.Connection> connectionsGenerated = erdosRenyiGeneratingConnections(numberOfSites, weightedQuickUnion);
        double runningTimeWeightedQuickUnion = timer.elapsedTime();
        // Weighted QuickUnion with path compression
        UF weightedQuickUnionPathCompression = new Exercise13_WeightedQUPathCompression().new WeightedQuickUnionPathCompression(numberOfSites);
        timer = new Stopwatch();
        erdosRenyiUsingConnections(weightedQuickUnionPathCompression, connectionsGenerated);
        double runningTimeWeightedQuickUnionPathCompression = timer.elapsedTime();
        Experiment experiment = new Experiment(numberOfSites, runningTimeWeightedQuickUnionPathCompression / runningTimeWeightedQuickUnion);
        experiments.add(experiment);
        numberOfSites *= 2;
    }
    printResults(experiments);
}
Also used : ArrayList(java.util.ArrayList) 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