Dynamically allocated arrays or std::vector

前端 未结 10 2171
日久生厌
日久生厌 2021-01-12 21:46

I\'m trying to optimize my C++ code. I\'ve searched the internet on using dynamically allocated C++ arrays vs using std::vector and have generally seen a recommendation in f

10条回答
  •  清歌不尽
    2021-01-12 22:38

    I imagine the reason why you found iterating and adding to std::vector 3 times slower than a plain array is a combination of the cost of iterating the vector and doing the assigment.

    Edit:

    That was my initial assumption before the testcase; however running the testcase (compiled with -O3) shows the converse - std::vector is actually 3 times faster, which surprised me.

    I can't see how std::vector could be faster (certainly not 3 times faster) than a vanilla array copy - I think there's some optimisation being applied to the std::vector compiled code which isn't happening for the array version.

    Original benchmark results:

    $ ./array
    array:  0.059375
    vector: 0.021209
    
    • std::vector is 3x faster. Same benchmark again, except add an additional outer loop to run the test iterater loop 1000 times:

      $ ./array array: 21.7129 vector: 21.6413

    • std::vector is now ~ the same speed as array.

    Edit 2

    Found it! So the problem with your test case is that in the vector case the memory holding the data appears to be already in the CPU cache - either by the way it is initialised, or due to the call to vec.end(). If I 'warm' up the CPU cache before each timing test, I get the same numbers for array and vector:

    #include 
    #include 
    #include 
    
    int main() {
      clock_t start,end;
      std::vector vec(9999999);
      std::vector::iterator vecIt = vec.begin();
      std::vector::iterator vecEnd = vec.end();
    
      // get vec into CPU cache.
      for (int i = 0; vecIt != vecEnd; i++) { *(vecIt++) = i; }
      vecIt = vec.begin();
      start = clock();
      for (int i = 0; vecIt != vecEnd; i++) {
        *(vecIt++) = i;
      }
      end = clock();
      std::cout<<"vector: "<<(double)(end-start)/CLOCKS_PER_SEC<

    This gives me the following result:

    $ ./array
    vector: 0.020875
    array: 0.020695
    

提交回复
热议问题