Using arrays or std::vectors in C++, what's the performance gap?

后端 未结 19 1459
忘了有多久
忘了有多久 2020-11-22 03:23

In our C++ course they suggest not to use C++ arrays on new projects anymore. As far as I know Stroustroup himself suggests not to use arrays. But are there significant perf

相关标签:
19条回答
  • 2020-11-22 04:05

    If you're using vectors to represent multi-dimensional behavior, there is a performance hit.

    Do 2d+ vectors cause a performance hit?

    The gist is that there's a small amount of overhead with each sub-vector having size information, and there will not necessarily be serialization of data (as there is with multi-dimensional c arrays). This lack of serialization can offer greater than micro optimization opportunities. If you're doing multi-dimensional arrays, it may be best to just extend std::vector and roll your own get/set/resize bits function.

    0 讨论(0)
  • 2020-11-22 04:06

    STL is a heavily optimized library. In fact, it's even suggested to use STL in games where high performance might be needed. Arrays are too error prone to be used in day to day tasks. Today's compilers are also very smart and can really produce excellent code with STL. If you know what you are doing, STL can usually provide the necessary performance. For example by initializing vectors to required size (if you know from start), you can basically achieve the array performance. However, there might be cases where you still need arrays. When interfacing with low level code (i.e. assembly) or old libraries that require arrays, you might not be able to use vectors.

    0 讨论(0)
  • 2020-11-22 04:07

    Go with STL. There's no performance penalty. The algorithms are very efficient and they do a good job of handling the kinds of details that most of us would not think about.

    0 讨论(0)
  • 2020-11-22 04:07

    Sometimes arrays are indeed better than vectors. If you are always manipulating a fixed length set of objects, arrays are better. Consider the following code snippets:

    int main() {
    int v[3];
    v[0]=1; v[1]=2;v[2]=3;
    int sum;
    int starttime=time(NULL);
    cout << starttime << endl;
    for (int i=0;i<50000;i++)
    for (int j=0;j<10000;j++) {
    X x(v);
    sum+=x.first();
    }
    int endtime=time(NULL);
    cout << endtime << endl;
    cout << endtime - starttime << endl;
    
    }
    

    where the vector version of X is

    class X {
    vector<int> vec;
    public:
    X(const vector<int>& v) {vec = v;}
    int first() { return vec[0];}
    };
    

    and the array version of X is:

    class X {
    int f[3];
    
    public:
    X(int a[]) {f[0]=a[0]; f[1]=a[1];f[2]=a[2];}
    int first() { return f[0];}
    };
    

    The array version will of main() will be faster because we are avoiding the overhead of "new" everytime in the inner loop.

    (This code was posted to comp.lang.c++ by me).

    0 讨论(0)
  • 2020-11-22 04:09

    If you compile the software in debug mode, many compilers will not inline the accessor functions of the vector. This will make the stl vector implementation much slower in circumstances where performance is an issue. It will also make the code easier to debug since you can see in the debugger how much memory was allocated.

    In optimized mode, I would expect the stl vector to approach the efficiency of an array. This is since many of the vector methods are now inlined.

    0 讨论(0)
  • 2020-11-22 04:13

    Assuming a fixed-length array (e.g. int* v = new int[1000]; vs std::vector<int> v(1000);, with the size of v being kept fixed at 1000), the only performance consideration that really matters (or at least mattered to me when I was in a similar dilemma) is the speed of access to an element. I looked up the STL's vector code, and here is what I found:

    const_reference
    operator[](size_type __n) const
    { return *(this->_M_impl._M_start + __n); }
    

    This function will most certainly be inlined by the compiler. So, as long as the only thing that you plan to do with v is access its elements with operator[], it seems like there shouldn't really be any difference in performance.

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