简体   繁体   English

O(log(n)) function 的运行时间

[英]Runtime of O(log(n)) function

I made an AVL tree in C++, and the tree is kept balanced successfully according to all of my tests.我在 C++ 中制作了一个 AVL 树,根据我的所有测试,该树成功保持平衡。 The function addNode should work in O(log(n)) (when n is the number of nodes in the tree), and it seems like my implementation satisfies it. function addNode应该在 O(log(n)) 中工作(当 n 是树中的节点数时),我的实现似乎满足它。 To verify, I wrote the following test:为了验证,我编写了以下测试:

#include "AVLTree.h"
#include "iostream"
#include <chrono>
#include <vector>
#include <random>
#include <algorithm>
#include <memory>

using namespace std;
using namespace std::chrono;

typedef high_resolution_clock Clock;
typedef Clock::time_point ClockTime;

auto ExecutionTime(ClockTime start_time, ClockTime end_time)
{
    return duration_cast<nanoseconds>(end_time - start_time).count();
}

#define N 10000000
#define M 100000

int main(){
    AVLTree<unsigned long long, unsigned long long> tree; // AVLTree<key type, value type>
    ClockTime start_time;
    ClockTime end_time;

    std::vector<unsigned long long> vector;
    for (unsigned long long i=0; i<N; i++) vector.push_back(i);

    auto max_time = ExecutionTime(start_time, start_time); // currently zero, will get bigger.

    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    shuffle (vector.begin(), vector.end(), std::default_random_engine(seed));

    unsigned long long counter = 0;
    for (auto i : vector){
        start_time = Clock::now();
        tree.addNode(i, i);
        end_time = Clock::now();
        max_time = max(max_time, ExecutionTime(start_time, end_time));
        counter++;
        if (counter == M){
            cout << "Small add: " << max_time << endl;
        }
    }
    cout << "Add: " << max_time << end;
}

Because the function works in θ(log(n)), for sufficiently large n, T(addNode)<clog(n) (T is the time function) for some constant c, and if we take a really tight one, we may assume T(addNode)=clog(n) for large enough n.因为 function 在 θ(log(n)) 中工作,对于足够大的 n,T(addNode)<clog(n)(T 是时间函数)对于某个常数 c,如果我们采取一个非常紧的,我们可以对于足够大的 n,假设 T(addNode)=clog(n)。 That means that the above test should output:也就是说上面的测试应该是output:

Small add: x
Add: (<2x)

For some x, and most time it does, but I also got对于某些 x,并且大多数时候确实如此,但我也得到了

Small add: 280100
Add: 14432000

Which is almost 100 times bigger.这几乎是 100 倍大。 Assuming the implementation is correct, why did it happen (it happens pretty rarely)?假设实现是正确的,为什么会发生(它很少发生)?

There are many things going on in you code besides the actual addNode logic.除了实际的addNode逻辑之外,您的代码中还发生了许多事情。 These thigs are done 100 times more in the M case:M案例中,这些事情要多做 100 倍:

  • Copying the next vector element to the i variable (use const auto& )将下一个向量元素复制到i变量(使用const auto&
  • Execution of Clock::now() which god know how many calls it does执行Clock::now()上帝知道它做了多少次调用
  • Calling the ExecutionTime function which uses an obscure amount of time to execute调用ExecutionTime function 使用模糊的时间来执行

Now consider if the running time of your addNode is in O(log(n)) the effect of the above actions become more and more dominant as the size grows.现在考虑如果您的addNode的运行时间在O(log(n))中,那么随着大小的增长,上述操作的效果变得越来越显着。 To check this simply comment the addNode line and see the results.要检查这一点,只需注释addNode行并查看结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM