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

后端 未结 22 2742
南方客
南方客 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:11

    I just want to mention that vector (and smart_ptr) is just a thin layer add on top of raw arrays (and raw pointers). And actually the access time of an vector in continuous memory is faster than array. The following code shows the result of initialize and access vector and array.

    #include 
    #include 
    #include 
    #define SIZE 20000
    int main() {
        srand (time(NULL));
        vector> vector2d;
        vector2d.reserve(SIZE);
        int index(0);
        boost::posix_time::ptime start_total = boost::posix_time::microsec_clock::local_time();
        //  timer start - build + access
        for (int i = 0; i < SIZE; i++) {
            vector2d.push_back(vector(SIZE));
        }
        boost::posix_time::ptime start_access = boost::posix_time::microsec_clock::local_time();
        //  timer start - access
        for (int i = 0; i < SIZE; i++) {
            index = rand()%SIZE;
            for (int j = 0; j < SIZE; j++) {
    
                vector2d[index][index]++;
            }
        }
        boost::posix_time::ptime end = boost::posix_time::microsec_clock::local_time();
        boost::posix_time::time_duration msdiff = end - start_total;
        cout << "Vector total time: " << msdiff.total_milliseconds() << "milliseconds.\n";
        msdiff = end - start_acess;
        cout << "Vector access time: " << msdiff.total_milliseconds() << "milliseconds.\n"; 
    
    
        int index(0);
        int** raw2d = nullptr;
        raw2d = new int*[SIZE];
        start_total = boost::posix_time::microsec_clock::local_time();
        //  timer start - build + access
        for (int i = 0; i < SIZE; i++) {
            raw2d[i] = new int[SIZE];
        }
        start_access = boost::posix_time::microsec_clock::local_time();
        //  timer start - access
        for (int i = 0; i < SIZE; i++) {
            index = rand()%SIZE;
            for (int j = 0; j < SIZE; j++) {
    
                raw2d[index][index]++;
            }
        }
        end = boost::posix_time::microsec_clock::local_time();
        msdiff = end - start_total;
        cout << "Array total time: " << msdiff.total_milliseconds() << "milliseconds.\n";
        msdiff = end - start_acess;
        cout << "Array access time: " << msdiff.total_milliseconds() << "milliseconds.\n"; 
        for (int i = 0; i < SIZE; i++) {
            delete [] raw2d[i];
        }
        return 0;
    }
    

    The output is:

        Vector total time: 925milliseconds.
        Vector access time: 4milliseconds.
        Array total time: 30milliseconds.
        Array access time: 21milliseconds.
    

    So the speed will be almost the same if you use it properly. (as others mentioned using reserve() or resize()).

提交回复
热议问题