Search in sources :

Example 31 with Stopwatch

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

the class Exercise32_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 };
    StdOut.printf("%18s %20s %10s\n", "Large input | ", "Number of queries | ", "Time spent");
    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);
            Stopwatch stopwatch = new Stopwatch();
            lookupCSV.lookup(filePaths[f], randomKeyIndex, randomValueIndex, numberOfQueries[q]);
            double timeSpent = stopwatch.elapsedTime();
            printResults(fileNames[f], numberOfQueries[q], timeSpent);
        }
    }
}
Also used : In(edu.princeton.cs.algs4.In) Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 32 with Stopwatch

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

the class Exercise34_SparseVector method doExperiment.

private void doExperiment() {
    int size = 10000;
    double[][] arrayMatrixManyNonZeroes = new double[size][size];
    double[][] arrayMatrixManyZeroes = new double[size][size];
    double[] arrayVectorManyNonZeroes = new double[size];
    double[] arrayVectorManyZeroes = new double[size];
    SparseMatrix sparseMatrixManyNonZeroes = new SparseMatrix(size, size);
    SparseMatrix sparseMatrixManyZeroes = new SparseMatrix(size, size);
    SparseVector sparseVectorManyNonZeroes = new SparseVector(size);
    SparseVector sparseVectorManyZeroes = new SparseVector(size);
    // Most values are nonzero
    for (int row = 0; row < size; row++) {
        for (int column = 0; column < size; column++) {
            int isZero = StdRandom.uniform(100);
            // 95% chance of being nonzero
            boolean isNonzeroValue = isZero <= 94;
            double value = 0;
            if (isNonzeroValue) {
                value = StdRandom.uniform();
            }
            arrayMatrixManyNonZeroes[row][column] = value;
            sparseMatrixManyNonZeroes.put(row, column, value);
        }
    }
    // Most values are zero
    for (int row = 0; row < size; row++) {
        for (int column = 0; column < size; column++) {
            int isZero = StdRandom.uniform(100);
            // 5% chance of being nonzero
            boolean isNonzeroValue = isZero >= 95;
            double value = 0;
            if (isNonzeroValue) {
                value = StdRandom.uniform();
            }
            arrayMatrixManyZeroes[row][column] = value;
            sparseMatrixManyZeroes.put(row, column, value);
        }
    }
    // Most values are nonzero
    for (int column = 0; column < size; column++) {
        int isZero = StdRandom.uniform(100);
        // 95% chance of being nonzero
        boolean isNonzeroValue = isZero <= 94;
        double value = 0;
        if (isNonzeroValue) {
            value = StdRandom.uniform();
        }
        arrayVectorManyNonZeroes[column] = value;
        sparseVectorManyNonZeroes.put(column, value);
    }
    // Most values are zero
    for (int column = 0; column < size; column++) {
        int isZero = StdRandom.uniform(100);
        // 5% chance of being nonzero
        boolean isNonzeroValue = isZero >= 95;
        double value = 0;
        if (isNonzeroValue) {
            value = StdRandom.uniform();
        }
        arrayVectorManyZeroes[column] = value;
        sparseVectorManyZeroes.put(column, value);
    }
    StdOut.printf("%17s %18s %20s %10s\n", "Method | ", "Matrix type | ", "Vector type | ", "Time spent");
    String[] methods = { "Arrays", "Sparse Vectors" };
    String[] types = { "Many non-zeroes", "Many zeroes" };
    // Array matrix many non-zeroes X array vector many non-zeroes
    Stopwatch stopwatch = new Stopwatch();
    dot(arrayMatrixManyNonZeroes, arrayVectorManyNonZeroes);
    double timeSpent = stopwatch.elapsedTime();
    printResults(methods[0], types[0], types[0], timeSpent);
    // Array matrix many non-zeroes X array vector many zeroes
    stopwatch = new Stopwatch();
    dot(arrayMatrixManyNonZeroes, arrayVectorManyZeroes);
    timeSpent = stopwatch.elapsedTime();
    printResults(methods[0], types[0], types[1], timeSpent);
    // Array matrix many zeroes X array vector many non-zeroes
    stopwatch = new Stopwatch();
    dot(arrayMatrixManyZeroes, arrayVectorManyNonZeroes);
    timeSpent = stopwatch.elapsedTime();
    printResults(methods[0], types[1], types[0], timeSpent);
    // Array matrix many zeroes X array vector many zeroes
    stopwatch = new Stopwatch();
    dot(arrayMatrixManyZeroes, arrayVectorManyZeroes);
    timeSpent = stopwatch.elapsedTime();
    printResults(methods[0], types[1], types[1], timeSpent);
    // Sparse matrix many non-zeroes X sparse vector many non-zeroes
    stopwatch = new Stopwatch();
    sparseMatrixManyNonZeroes.dot(sparseVectorManyNonZeroes);
    timeSpent = stopwatch.elapsedTime();
    printResults(methods[1], types[0], types[0], timeSpent);
    // Sparse matrix many non-zeroes X sparse vector many zeroes
    stopwatch = new Stopwatch();
    sparseMatrixManyNonZeroes.dot(sparseVectorManyZeroes);
    timeSpent = stopwatch.elapsedTime();
    printResults(methods[1], types[0], types[1], timeSpent);
    // Sparse matrix many zeroes X sparse vector many non-zeroes
    stopwatch = new Stopwatch();
    sparseMatrixManyZeroes.dot(sparseVectorManyNonZeroes);
    timeSpent = stopwatch.elapsedTime();
    printResults(methods[1], types[1], types[0], timeSpent);
    // Sparse matrix many zeroes X sparse vector many zeroes
    stopwatch = new Stopwatch();
    sparseMatrixManyZeroes.dot(sparseVectorManyZeroes);
    timeSpent = stopwatch.elapsedTime();
    printResults(methods[1], types[1], types[1], timeSpent);
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 33 with Stopwatch

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

the class Exercise35_PrimitiveTypes method doExperiment.

private void doExperiment(int[] numberOfSizesAndQueries, int[] randomIntKeys, double[] randomDoubleKeys, int[] queriesInt, double[] queriesDouble) {
    StdOut.printf("%17s %12s %20s %10s\n", "Symbol table | ", "Key type | ", "Size and queries | ", "Time spent");
    // Linear probing hash table with Integer keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        LinearProbingHashTable<Integer, Integer> linearProbingHashTableInteger = new LinearProbingHashTable<>(997);
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTableInteger.put(randomIntKeys[j], randomIntKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTableInteger.get(queriesInt[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(LINEAR_PROBING_SYMBOL_TABLE_TYPE, INTEGER_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Linear probing hash table with int keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        Exercise4.HashSTint<Integer> linearProbingHashTableint = new Exercise4().new HashSTint<>(997);
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTableint.put(randomIntKeys[j], randomIntKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTableint.get(queriesInt[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(LINEAR_PROBING_SYMBOL_TABLE_TYPE, PRIMITIVE_INT_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Linear probing hash table with Double keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        LinearProbingHashTable<Double, Double> linearProbingHashTableDouble = new LinearProbingHashTable<>(997);
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTableDouble.put(randomDoubleKeys[j], randomDoubleKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTableDouble.get(queriesDouble[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(LINEAR_PROBING_SYMBOL_TABLE_TYPE, DOUBLE_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Linear probing hash table with double keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        Exercise4.HashSTdouble<Double> linearProbingHashTabledouble = new Exercise4().new HashSTdouble<>(997);
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTabledouble.put(randomDoubleKeys[j], randomDoubleKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTabledouble.get(queriesDouble[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(LINEAR_PROBING_SYMBOL_TABLE_TYPE, PRIMITIVE_DOUBLE_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Red-black BST with Integer keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        RedBlackBST<Integer, Integer> redBlackBSTInteger = new RedBlackBST<>();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTInteger.put(randomIntKeys[j], randomIntKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTInteger.get(queriesInt[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(RED_BLACK_BST_SYMBOL_TABLE_TYPE, INTEGER_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Red-black BST with int keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        Exercise5.STint<Integer> redBlackBSTint = new Exercise5().new STint<>();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTint.put(randomIntKeys[j], randomIntKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTint.get(queriesInt[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(RED_BLACK_BST_SYMBOL_TABLE_TYPE, PRIMITIVE_INT_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Red-black BST with Double keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        RedBlackBST<Double, Double> redBlackBSTDouble = new RedBlackBST<>();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTDouble.put(randomDoubleKeys[j], randomDoubleKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTDouble.get(queriesDouble[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(RED_BLACK_BST_SYMBOL_TABLE_TYPE, DOUBLE_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Red-black BST with double keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        Exercise5.STdouble<Double> redBlackBSTdouble = new Exercise5().new STdouble<>();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTdouble.put(randomDoubleKeys[j], randomDoubleKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTdouble.get(queriesDouble[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(RED_BLACK_BST_SYMBOL_TABLE_TYPE, PRIMITIVE_DOUBLE_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch) LinearProbingHashTable(chapter3.section4.LinearProbingHashTable) RedBlackBST(chapter3.section3.RedBlackBST)

Example 34 with Stopwatch

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

the class Exercise42_DoubleHashing method doExperiment.

private void doExperiment() {
    LinearProbingHashTable<Integer, Integer> linearProbingHashTable = new LinearProbingHashTable<>(10);
    DoubleHashingHashTable<Integer, Integer> doubleHashingHashTable = new DoubleHashingHashTable<>(10);
    StdOut.printf("%12s %20s %20s\n", "Operation | ", "Linear-probing HT time | ", "Double hashing HT time");
    // Put tests
    int[] randomKeysPut = new int[1000000];
    for (int i = 0; i < randomKeysPut.length; i++) {
        int randomKey = StdRandom.uniform(Integer.MAX_VALUE);
        randomKeysPut[i] = randomKey;
    }
    Stopwatch stopwatch = new Stopwatch();
    for (int i = 0; i < randomKeysPut.length; i++) {
        linearProbingHashTable.put(randomKeysPut[i], randomKeysPut[i]);
    }
    double timeSpentOnPutLinearProbing = stopwatch.elapsedTime();
    stopwatch = new Stopwatch();
    for (int i = 0; i < randomKeysPut.length; i++) {
        doubleHashingHashTable.put(randomKeysPut[i], randomKeysPut[i]);
    }
    double timeSpentOnPutDoubleHashing = stopwatch.elapsedTime();
    printResults("Put", timeSpentOnPutLinearProbing, timeSpentOnPutDoubleHashing);
    // Get tests
    int[] randomKeysGet = new int[500000];
    for (int i = 0; i < randomKeysGet.length; i++) {
        int randomKey = StdRandom.uniform(Integer.MAX_VALUE);
        randomKeysGet[i] = randomKey;
    }
    stopwatch = new Stopwatch();
    for (int i = 0; i < randomKeysGet.length; i++) {
        linearProbingHashTable.get(randomKeysGet[i]);
    }
    double timeSpentOnGetLinearProbing = stopwatch.elapsedTime();
    stopwatch = new Stopwatch();
    for (int i = 0; i < randomKeysGet.length; i++) {
        doubleHashingHashTable.get(randomKeysGet[i]);
    }
    double timeSpentOnGetDoubleHashing = stopwatch.elapsedTime();
    printResults("Get", timeSpentOnGetLinearProbing, timeSpentOnGetDoubleHashing);
    // Delete tests
    stopwatch = new Stopwatch();
    for (int i = 0; i < randomKeysPut.length / 2; i++) {
        linearProbingHashTable.delete(randomKeysPut[i]);
    }
    double timeSpentOnDeleteLinearProbing = stopwatch.elapsedTime();
    stopwatch = new Stopwatch();
    for (int i = 0; i < randomKeysPut.length / 2; i++) {
        doubleHashingHashTable.delete(randomKeysPut[i]);
    }
    double timeSpentOnDeleteDoubleHashing = stopwatch.elapsedTime();
    printResults("Delete", timeSpentOnDeleteLinearProbing, timeSpentOnDeleteDoubleHashing);
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 35 with Stopwatch

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

the class Exercise30_DuplicatesRevisited method countDistinctValues.

public void countDistinctValues(boolean useDedup, boolean generateLongValues, int[] values) {
    int numberOfTrials = 10;
    String method;
    String valuesType;
    if (useDedup) {
        method = "DeDup";
    } else {
        method = "Array index count";
    }
    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) {
                        distinctValues = distinctCounter.deDupWithIntegerValues(numberOfValues, maxValue);
                    } else {
                        distinctValues = distinctCounter.deDupWithLongValues(numberOfValues, maxValue);
                    }
                }
            }
            double timeSpent = stopwatch.elapsedTime();
            printResults(method, valuesType, numberOfValues, maxValue, timeSpent);
        }
    }
}
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