C++: Optimizing speed with vector/array?

前端 未结 8 1736
半阙折子戏
半阙折子戏 2021-01-02 00:51

I have a nested for-loop structure and right now I am re-declaring the vector at the start of each iteration:

void function (n1,n2,bound,etc){

    for (int          


        
相关标签:
8条回答
  • 2021-01-02 01:17

    In addition to the previous comments :

    if you use Robinson's swap method, you could go ever faster by handling that swap asynchronously.

    0 讨论(0)
  • 2021-01-02 01:18

    When choosing a container i usually use this diagram to help me:

    enter image description here

    source


    Other than that,

    Like previously posted if this is causing performance problems declare the container outside of the for loop and just clear it at the start of each iteration

    0 讨论(0)
  • 2021-01-02 01:18

    The overhead of using a vector vs an array is minor, especially when you are getting a lot of useful functionality from the vector. Internally a vector allocates an array. So vector is the way to go.

    0 讨论(0)
  • 2021-01-02 01:22

    Why not something like that :

    {
        vector< vector<long long> > vec(n1, vector<long long>(n2));
    
        for (int i=0; i<bound; i++){
    
             //about three more for-loops here
    
             vec.clear();
        }
    }
    

    Edit: added scope braces ;-)

    0 讨论(0)
  • 2021-01-02 01:29

    Here is some code that tests a few different methods.

    #include <chrono>
    #include <iostream>
    #include <vector>
    
    int main()
    {
      typedef std::chrono::high_resolution_clock clock;
    
      unsigned n1 = 1000;
      unsigned n2 = 1000;
    
      // Original method
      {
        auto start = clock::now();
        for (unsigned i = 0; i < 10000; ++i)
        {
          std::vector<std::vector<long long>> vec(n1, std::vector<long long>(n2));
          // vec is initialized to zero already
    
          // do stuff
        }
        auto elapsed_time = clock::now() - start;
    
        std::cout << elapsed_time.count() << std::endl;
      }
    
    
      // reinitialize values to zero at every pass in the loop
      {
        auto start = clock::now();
        std::vector<std::vector<long long>> vec(n1, std::vector<long long>(n2));
        for (unsigned i = 0; i < 10000; ++i)
        {
          // initialize vec to zero at the start of every loop
          for (unsigned j = 0; j < n1; ++j)
            for (unsigned k = 0; k < n2; ++k)
                vec[j][k] = 0;
    
          // do stuff
        }
        auto elapsed_time = clock::now() - start;
    
        std::cout << elapsed_time.count() << std::endl;
      }
    
      // clearing the vector this way is not optimal since it will destruct the
      // inner vectors
      {
        auto start = clock::now();
        std::vector<std::vector<long long>> vec(n1, std::vector<long long>(n2));
        for (unsigned i = 0; i < 10000; ++i)
        {
          vec.clear();
          vec.resize(n1, std::vector<long long>(n2));
    
          // do stuff
        }
        auto elapsed_time = clock::now() - start;
    
        std::cout << elapsed_time.count() << std::endl;
      }
    
      // equivalent to the second method from above
      // no performace penalty
      {
        auto start = clock::now();
        std::vector<std::vector<long long>> vec(n1, std::vector<long long>(n2));
        for (unsigned i = 0; i < 10000; ++i)
        {
          for (unsigned j = 0; j < n1; ++j)
          {
            vec[j].clear();
            vec[j].resize(n2);
          }
    
          // do stuff
        }
        auto elapsed_time = clock::now() - start;
    
        std::cout << elapsed_time.count() << std::endl;
      }
    }
    

    Edit: I've updated the code to make a fairer comparison between the methods. Edit 2: Cleaned up the code a bit, methods 2 or 4 are the way to go.

    Here are the timings of the above four methods on my computer:

    16327389
    15216024
    16371469
    15279471
    

    The point is that you should try out different methods and profile your code.

    0 讨论(0)
  • 2021-01-02 01:31

    I have a clear preference for small scopes (i.e. declaring the variable in the innermost loop if it’s only used there) but for large sizes this could cause a lot of allocations.

    So if this loop is a performance problem, try declaring the variable outside the loop and merely clearing it inside the loop – however, this is only advantageous if the (reserved) size of the vector stays identical. If you are resizing the vector, then you get reallocations anyway.

    Don’t use a raw array – it doesn’t give you any advantage, and only trouble.

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