Is std::vector so much slower than plain arrays?

后端 未结 22 2638
南方客
南方客 2020-11-22 12:00

I\'ve always thought it\'s the general wisdom that std::vector is \"implemented as an array,\" blah blah blah. Today I went down and tested it, and it seems to

22条回答
  •  没有蜡笔的小新
    2020-11-22 12:29

    Some profiler data (pixel is aligned to 32 bits):

    g++ -msse3 -O3 -ftree-vectorize -g test.cpp -DNDEBUG && ./a.out
    UseVector completed in 3.123 seconds
    UseArray completed in 1.847 seconds
    UseVectorPushBack completed in 9.186 seconds
    The whole thing completed in 14.159 seconds
    

    Blah

    andrey@nv:~$ opannotate --source libcchem/src/a.out  | grep "Total samples for file" -A3
    Overflow stats not available
     * Total samples for file : "/usr/include/c++/4.4/ext/new_allocator.h"
     *
     * 141008 52.5367
     */
    --
     * Total samples for file : "/home/andrey/libcchem/src/test.cpp"
     *
     *  61556 22.9345
     */
    --
     * Total samples for file : "/usr/include/c++/4.4/bits/stl_vector.h"
     *
     *  41956 15.6320
     */
    --
     * Total samples for file : "/usr/include/c++/4.4/bits/stl_uninitialized.h"
     *
     *  20956  7.8078
     */
    --
     * Total samples for file : "/usr/include/c++/4.4/bits/stl_construct.h"
     *
     *   2923  1.0891
     */
    

    In allocator:

                   :      // _GLIBCXX_RESOLVE_LIB_DEFECTS
                   :      // 402. wrong new expression in [some_] allocator::construct
                   :      void
                   :      construct(pointer __p, const _Tp& __val)
    141008 52.5367 :      { ::new((void *)__p) _Tp(__val); }
    

    vector:

                   :void UseVector()
                   :{ /* UseVector() total:  60121 22.3999 */
    ...
                   :
                   :
     10790  4.0201 :        for (int i = 0; i < dimension * dimension; ++i) {
                   :
       495  0.1844 :            pixels[i].r = 255;
                   :
     12618  4.7012 :            pixels[i].g = 0;
                   :
      2253  0.8394 :            pixels[i].b = 0;
                   :
                   :        }
    

    array

                   :void UseArray()
                   :{ /* UseArray() total:  35191 13.1114 */
                   :
    ...
                   :
       136  0.0507 :        for (int i = 0; i < dimension * dimension; ++i) {
                   :
      9897  3.6874 :            pixels[i].r = 255;
                   :
      3511  1.3081 :            pixels[i].g = 0;
                   :
     21647  8.0652 :            pixels[i].b = 0;
    

    Most of the overhead is in the copy constructor. For example,

        std::vector < Pixel > pixels;//(dimension * dimension, Pixel());
    
        pixels.reserve(dimension * dimension);
    
        for (int i = 0; i < dimension * dimension; ++i) {
    
            pixels[i].r = 255;
    
            pixels[i].g = 0;
    
            pixels[i].b = 0;
        }
    

    It has the same performance as an array.

提交回复
热议问题