Dynamically allocated arrays or std::vector

前端 未结 10 1881
日久生厌
日久生厌 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:25

    I agree with rmeador,

      for (int i = 0; vecIt != vecEnd; i++) {
        *(vecIt++) = i; // <-- quick offset calculation
      }
      end = clock();
      cout<<"vector: "<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
    
      int* arr = new int[9999999];
      start = clock();
      for (int i = 0; i < 9999999; i++) {
        arr[i] = i; // <-- not fair play :) - offset = arr + i*size(int)
      }
    
    0 讨论(0)
  • 2021-01-12 22:26

    The reason that your array iterating is faster is that the the number of iteration is constant, and compiler is able to unroll the loop. Try to use rand to generate a number, and multiple it to be a big number you wanted so that compiler wont be able to figure it out at compile time. Then try it again, you will see similar runtime results.

    0 讨论(0)
  • 2021-01-12 22:32

    When benchmarking C++ comtainers, it's important to enable most compiler optimisations. Several of my own answers on SO have fallen foul of this - for example, the function call overhead when something like operator[] is not inlined can be very significant.

    0 讨论(0)
  • 2021-01-12 22:35

    The issue seems to be that you compiled your code with optimizations turned off. On my machine, OS X 10.5.7 with g++ 4.0.1 I actually see that the vector is faster than primitive arrays by a factor of 2.5.

    With gcc try to pass -O2 to the compiler and see if there's any improvement.

    0 讨论(0)
  • 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 <time.h>
    #include <iostream>
    #include <vector>
    
    int main() {
      clock_t start,end;
      std::vector<int> vec(9999999);
      std::vector<int>::iterator vecIt = vec.begin();
      std::vector<int>::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<<std::endl;
    
      int* arr = new int[9999999];
    
      // get arr into CPU cache.
      for (int i = 0; i < 9999999; i++) { arr[i] = i; }
      start = clock();
      for (int i = 0; i < 9999999; i++) {
        arr[i] = i;
      }
      end = clock();
      std::cout<<"array: "<<(double)(end-start)/CLOCKS_PER_SEC<<std::endl;
    }
    

    This gives me the following result:

    $ ./array
    vector: 0.020875
    array: 0.020695
    
    0 讨论(0)
  • 2021-01-12 22:42

    Just for fun, try iterating over the plain array using a pointer instead of an integer index (the code should look just like the vector iteration, since the point of STL iterators is to appear like pointer arithmetic for most operations). I bet the speed will be exactly equal in that case. Which of course means you should pick the vector, since it will save you a world of headaches from managing arrays by hand.

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