Search in sources :

Example 36 with Stopwatch

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

the class Exercise31_SpellChecker method doExperiment.

// There was no dictionary.txt file on the booksite, so I suspect it has been renamed to commonwords.txt
// Parameter example: common_words.txt
private void doExperiment(String[] args) {
    String dictionaryFileName = args[0];
    String dictionaryFilePath = Constants.FILES_PATH + dictionaryFileName;
    String warAndPeaceFilePath = Constants.FILES_PATH + Constants.WAR_AND_PEACE_FILE;
    BlackFilter blackFilter = new BlackFilter();
    Stopwatch stopwatch = new Stopwatch();
    blackFilter.filterUsingRedBlackBST(dictionaryFilePath, warAndPeaceFilePath);
    double timeSpentWithRedBlackBST = stopwatch.elapsedTime();
    stopwatch = new Stopwatch();
    blackFilter.filterUsingSeparateChainingHashST(dictionaryFilePath, warAndPeaceFilePath);
    double timeSpentWithSeparateChainingST = stopwatch.elapsedTime();
    stopwatch = new Stopwatch();
    blackFilter.filterUsingLinearProbingHashST(dictionaryFilePath, warAndPeaceFilePath);
    double timeSpentWithLinearProbingST = stopwatch.elapsedTime();
    StdOut.printf("%20s %20s %20s\n", "Time spent red-black BST | ", "Time spent separate chaining | ", "Time spent linear probing");
    printResults(timeSpentWithRedBlackBST, timeSpentWithSeparateChainingST, timeSpentWithLinearProbingST);
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 37 with Stopwatch

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

the class Exercise15 method computePercentageTimeSpentOnInsertions.

private void computePercentageTimeSpentOnInsertions() {
    int[] numberOfSearches = { 1000, 1000000, 1000000000 };
    BinarySearchSymbolTable<Integer, String> binarySearchSymbolTable = new BinarySearchSymbolTable<>();
    for (int i = 0; i < 10000; i++) {
        binarySearchSymbolTable.put(i, "Value " + i);
    }
    StdOut.printf("%13s %25s\n", "Searches", "Percentage of total time spent on insertions");
    for (int i = 0; i < numberOfSearches.length; i++) {
        int numberOfInsertions = numberOfSearches[i] / 1000;
        Stopwatch totalTimeTimer = new Stopwatch();
        for (int search = 0; search < numberOfSearches[i]; search++) {
            int randomValueToSearch = StdRandom.uniform(10000);
            binarySearchSymbolTable.get(randomValueToSearch);
        }
        Stopwatch insertionsTimer = new Stopwatch();
        for (int insertion = 0; insertion < numberOfInsertions; insertion++) {
            int randomValueToInsert = StdRandom.uniform(100000);
            binarySearchSymbolTable.put(randomValueToInsert, "Value " + randomValueToInsert);
        }
        double insertionsTime = insertionsTimer.elapsedTime();
        double totalTime = totalTimeTimer.elapsedTime();
        double percentageOfTimeOnInsertions = (insertionsTime / totalTime) * 100;
        printResults(numberOfSearches[i], percentageOfTimeOnInsertions);
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 38 with Stopwatch

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

the class Exercise24_SpellChecker method doExperiment.

// There was no dictionary.txt file on the booksite, so I suspect it has been renamed to commonwords.txt
// Parameter example: common_words.txt
private void doExperiment(String[] args) {
    String dictionaryFileName = args[0];
    String dictionaryFilePath = Constants.FILES_PATH + dictionaryFileName;
    String warAndPeaceFilePath = Constants.FILES_PATH + Constants.WAR_AND_PEACE_FILE;
    BlackFilter blackFilter = new BlackFilter();
    Stopwatch stopwatch = new Stopwatch();
    blackFilter.filterUsingTrie(dictionaryFilePath, warAndPeaceFilePath);
    double timeSpentWithTrie = stopwatch.elapsedTime();
    stopwatch = new Stopwatch();
    blackFilter.filterUsingTST(dictionaryFilePath, warAndPeaceFilePath);
    double timeSpentWithTST = stopwatch.elapsedTime();
    StdOut.printf("%19s %14s\n", "Time spent trie | ", "Time spent TST");
    printResults(timeSpentWithTrie, timeSpentWithTST);
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 39 with Stopwatch

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

the class Exercise39_Timings method doExperiment.

private void doExperiment() {
    String taleOfTwoCitiesFile = Constants.FILES_PATH + Constants.TALE_OF_TWO_CITIES_FILE;
    String taleOfTwoCitiesText = FileUtil.getAllCharactersFromFile(taleOfTwoCitiesFile, true);
    String pattern = "it is a far far better thing that I do than I have ever done";
    String[] substringSearchMethods = { SubstringSearch.BRUTEFORCE_METHOD, SubstringSearch.KNUTH_MORRIS_PRATT_METHOD, SubstringSearch.BOYER_MOORE_METHOD, SubstringSearch.RABIN_KARP_METHOD };
    StdOut.printf("%20s %10s\n", "Method |", "Time spent");
    for (int substringSearchMethod = 0; substringSearchMethod < substringSearchMethods.length; substringSearchMethod++) {
        SubstringSearch substringSearch;
        switch(substringSearchMethod) {
            case SubstringSearch.BRUTEFORCE:
                substringSearch = new BruteForceSubstringSearch(pattern);
                break;
            case SubstringSearch.KNUTH_MORRIS_PRATT:
                substringSearch = new KnuthMorrisPratt(pattern);
                break;
            case SubstringSearch.BOYER_MOORE:
                substringSearch = new BoyerMoore(pattern);
                break;
            default:
                substringSearch = new RabinKarp(pattern, true);
                break;
        }
        Stopwatch stopwatch = new Stopwatch();
        substringSearch.search(taleOfTwoCitiesText);
        double timeSpent = stopwatch.elapsedTime();
        printResults(substringSearchMethods[substringSearchMethod], timeSpent);
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 40 with Stopwatch

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

the class Exercise41_DoubleProbing method doExperiment.

private void doExperiment() {
    SeparateChainingHashTable<Integer, Integer> separateChainingHashTable = new SeparateChainingHashTable<>();
    DoubleProbingHashTable<Integer, Integer> doubleProbingHashTable = new DoubleProbingHashTable<>();
    StdOut.printf("%12s %20s %20s\n", "Operation | ", "Separate-chaining HT time | ", "Double probing 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++) {
        separateChainingHashTable.put(randomKeysPut[i], randomKeysPut[i]);
    }
    double timeSpentOnPutSeparateChaining = stopwatch.elapsedTime();
    stopwatch = new Stopwatch();
    for (int i = 0; i < randomKeysPut.length; i++) {
        doubleProbingHashTable.put(randomKeysPut[i], randomKeysPut[i]);
    }
    double timeSpentOnPutDoubleProbing = stopwatch.elapsedTime();
    printResults("Put", timeSpentOnPutSeparateChaining, timeSpentOnPutDoubleProbing);
    // 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++) {
        separateChainingHashTable.get(randomKeysGet[i]);
    }
    double timeSpentOnGetSeparateChaining = stopwatch.elapsedTime();
    stopwatch = new Stopwatch();
    for (int i = 0; i < randomKeysGet.length; i++) {
        doubleProbingHashTable.get(randomKeysGet[i]);
    }
    double timeSpentOnGetDoubleProbing = stopwatch.elapsedTime();
    printResults("Get", timeSpentOnGetSeparateChaining, timeSpentOnGetDoubleProbing);
    // Delete tests
    stopwatch = new Stopwatch();
    for (int i = 0; i < randomKeysPut.length / 2; i++) {
        separateChainingHashTable.delete(randomKeysPut[i]);
    }
    double timeSpentOnDeleteSeparateChaining = stopwatch.elapsedTime();
    stopwatch = new Stopwatch();
    for (int i = 0; i < randomKeysPut.length / 2; i++) {
        doubleProbingHashTable.delete(randomKeysPut[i]);
    }
    double timeSpentOnDeleteDoubleProbing = stopwatch.elapsedTime();
    printResults("Delete", timeSpentOnDeleteSeparateChaining, timeSpentOnDeleteDoubleProbing);
}
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