Is it more efficient to preallocate a vector?

后端 未结 3 1980
野的像风
野的像风 2020-12-13 17:54

In C++ Primer fourth edition, by Stanley B.Lippman, Josee Lajoie and Barbara E. Moo it states:

Because vectors grow efficiently, it is usually best to

3条回答
  •  一个人的身影
    2020-12-13 18:26

    I timed this simple example:

    #include
    #include
    
    int main() {
    
        int limit = 100 * 1000 * 1000;
        std::vector my_vec;
        my_vec.reserve(limit); // comment out this line to not preallocate
    
        for (int i=0; i < limit; i++) {
            my_vec.push_back(i);
        }
    
        long my_sum = 0;
        for (int i=0; i < limit; i++) {
            my_sum += my_vec[i];
        }
    
        std::cout << my_sum << std::endl;
        return 0;
    }
    

    Complied with:

    g++ -std=c++11 -O2 my_file.cpp -o my_exec
    

    And found the difference to be substantial:

    Without preallocation:

    real    0m3.366s
    user    0m1.656s
    sys     0m1.660s
    

    With preallocation:

    real    0m1.688s
    user    0m0.732s
    sys     0m0.936s
    

    My conclusion here is: If building a vector is a big part of the program, then preallocating for efficiency makes sense. However, building a larger vector over and over is unlikely, and thus it is rarely a bottle neck. However, using reserve() has other advantages besides preallocating.

    Bjarne Stroustrup in The C++ programming language (4th addition) has this to say:

    I used to be careful about using reserve() when I was reading into a vector. I was surprised to find that for essentially all my uses, calling reserve() did not measurably affect performance. The default growth strategy worked just as well as my estimates, so I stopped trying to improve performance using reserve(). Instead I use it to increase predictability of reallocation delays and to prevent invalidation of pointers and iterators.

提交回复
热议问题