Dynamically allocated arrays or std::vector

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

    The thing about the standard library classes such as std::vector is that yes, naively, it is a lot more code than a raw array. But all of it can be trivially inlined by the compiler, which means that if optimizations are enabled, it becomes essentially the same code as if you'd used a raw array. The speed difference then is not negligible but non-existent. All the overhead is removed at compile-time.

    But that requires compiler optimizations to be enabled.

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

    One reason you're code might not be performing quite the same is because on your std::vector version, you are incrimenting two values, the integer i and the std::vector::iterator vecIt. To really be equivalent, you could refactor to

    start = clock();
    for (int i = 0; i < vec.size(); i++) {
      vec[i] = i;
    }
    end = clock();
    cout<<"vector: "<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
    
    0 讨论(0)
  • 2021-01-12 22:46

    I think the answer here is obvious: it doesn't matter. Like jalf said the code will end up being about the same, but even if it wasn't, look at the numbers. The code you posted creates a huge array of 10 MILLION items, yet iterating over the entire array takes only a few hundredths of a second.

    Even if your application really is working with that much data, whatever it is you're actually doing with that data is likely to take much more time than iterating over your array. Just use whichever data structure you prefer, and focus your time on the rest of your code.

    To prove my point, here's the code with one change: the assignment of i to the array item is replaced with an assignment of sqrt(i). On my machine using -O2, the execution time triples from .02 to .06 seconds.

    #include <time.h>
    #include <iostream>
    #include <vector>
    #include <math.h>
    
    using namespace std;
    
    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();
    
      start = clock();
      for (int i = 0; vecIt != vecEnd; i++) {
        *(vecIt++) = sqrt(i);
      }
      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;
      }
      end = clock();
      cout<<"array: "<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
    }
    
    0 讨论(0)
  • 2021-01-12 22:48

    Your code provides an unfair comparison between the two cases since you're doing far more work in the vector test than the array test.

    With the vector, you're incrementing both the iterator (vecIT) and a separate variable (i) for generating the assignment values.

    With the array, you're only incrementing the variable i and using it for dual purpose.

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