How to know calculate the execution time of an algorithm in c++?

后端 未结 4 961
轻奢々
轻奢々 2021-01-07 15:25

I want to test which data structure is the best by looking at the run-time performance of my algorithm, how do I do it?

For example I already have a hashmap<

4条回答
  •  花落未央
    2021-01-07 15:47

    First of all take a look at my reply to this question; it contains a portable (windows/linux) function to get the time in milliseconds.

    Next, do something like this:

    int64 start_time = GetTimeMs64();
    const int NUM_TIMES = 100000; /* Choose this so it takes at the very least half a minute to run */
    
    for (int i = 0; i < NUM_TIMES; ++i) {
       /* Code you want to time.. */
    }
    
    double milliseconds = (GetTimeMs64() - start_time) / (double)NUM_TIMES;
    

    All done! (Note that I haven't tried to compile it)

提交回复
热议问题