Why do I get a constant instead of logarithmic curve for an insert time benchmark of the RB-tree based C++ std::set?

前端 未结 1 883
梦谈多话
梦谈多话 2021-02-20 06:41

I was comparing BST to Heap at: Heap vs Binary Search Tree (BST) but when I tried to benchmark both and compare results, I could not interpret the data for BST.

First, I

1条回答
  •  粉色の甜心
    2021-02-20 07:23

    The clock is likely not accurate enough as mentioned in the comments, so I've tried to group a bunch of inserts together and time them to improve the signal to noise, and it worked, I can see a logarithm now:

    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char **argv) {
        size_t i, j, n, granule;
        std::set bst;
        std::random_device dev;
        unsigned int seed = dev();
        std::mt19937 prng(seed);
        std::uniform_int_distribution<> dist;
        int *randoms;
    
        if (argc > 1) {
            n = std::stoi(argv[1]);
        } else {
            n = 1000000;
        }
        if (argc > 2) {
            granule = std::stoi(argv[2]);
        } else {
            granule = 10;
        }
        randoms = new int[granule];
        for (i = 0; i < n / granule; ++i) {
            for (j = 0; j < granule; ++j) {
                randoms[j] = dist(prng);
            }
            auto start = std::chrono::high_resolution_clock::now();
            for (j = 0; j < granule; ++j) {
                bst.insert(randoms[j]);
            }
            auto end = std::chrono::high_resolution_clock::now();
            auto dt_bst = end - start;
            std::cout << std::chrono::duration_cast(dt_bst).count() << std::endl;
        }
        delete[] randoms;
    }
    

    Command:

    ./main.out 100000000 10000
    

    Graph:

    0 讨论(0)
提交回复
热议问题