pearcej / cppds

Problem Solving with Algorithms and Data Structures using C++
Other
11 stars 76 forks source link

Listing 6 in Section 2.8 does not test the contains operation #325

Open DaveParillo opened 1 year ago

DaveParillo commented 1 year ago

Describe the bug Section 2.8 claims to compare vector and hash table "contains operations".

To Reproduce Steps to reproduce the behavior:

  1. Go to analysis of hash table
  2. Compile c++ listing 6 and run program
  3. Observe the output does not match the text or the graph

Sample actual results.

Expected behavior Expected results

Recommendations

#include <algorithm>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <unordered_map>
#include <vector>

using namespace std;

int main() {
    cout << std::setw(6) << "size\t"
         << std::setw(8) << "vector\t\t"
         << std::setw(8) << "hash table" << '\n';
    for(int a = 1000; a < 1e5; a += 1e4) {
        // vector Part
        vvector<int> avector (a);                        // reserve memory
        std::iota(avector.begin(), avector.end(), 0);    // fill vector
        // search vector
        auto begin = std::chrono::steady_clock::now();
        for( int i = 0; i < a; ++i){
            if(std::find(avector.begin(), avector.end(), i) == avector.end()) {
                std::cerr << "Failed to find an expected value! Halting.\n";
                return -1;
            }
        }
        auto end = std::chrono::steady_clock::now();
        std::chrono::duration<double> elapsed_secs = end - begin;
        // Hash Table Part
        unordered_map<int, int> table;
        for( int j = 0; j < a; ++j ){
            table[j] = j;
        }
        auto begin_ht = std::chrono::steady_clock::now();
        // search hash table
        for( int j = 0; j < a; ++j){
            if(table.find(j) == table.end()) {
                std::cerr << "Failed to find an expected value! Halting.\n";
                return -2;
            }
        }
        auto end_ht = std::chrono::steady_clock::now();
        std::chrono::duration<double> elapsed_secs_ht = end_ht - begin_ht;

        // Printing final output
        cout << std::setw(6) << a << '\t'
             << std::setw(8) << elapsed_secs.count() << '\t'
             << std::setw(8) << elapsed_secs_ht.count() << '\n';
    }
    return 0;
}
pearcej commented 11 months ago

@DaveParillo good idea! I welcome a pull-request... or I will have my students fix.

DaveParillo commented 10 months ago

I would be willing to work on this, but . . .

  1. it's not clear to me where the fix should be implemented. This repo appears to contain a runestone and a pretext implementation.
  2. When I build the runestone version of this book, I get a lot sphinx template stuff in rendered pages {{ if response.serve_ad and settings.adsenseid: }} {{ pass }} etc.
  3. Not sure how the images were generated. I can make new images for the timing results, but they won't look like the other images in this book.

If you are OK with me simply making changes to AlgorithmAnalysis/HashTableAnalysis.rst and verifying those changes are valid, then I can do that.

I am assuming you would rather replace std::chrono calls with the C time functions you use in other examples.