C + + standard library std::list and std::vector performance comparison __c++

Source: Internet
Author: User

Lists and vectors are implemented through linked lists and arrays, so the list is more efficient to delete and insert than vector, while vectors are higher in random access than the list, but the efficiency of both sequential additions and sequential traversal is high.

First analysis,

1 for sequential additions, when the vector is not allocating enough memory, the need to reallocate memory and replicate the object will negatively affect efficiency, and the list must be dynamically allocated every time an object is added, which consumes the CPU time of each dynamic allocation of memory. This is also a problem that seriously affects list efficiency, so the list can run less efficiently than the vector. From another angle, each object in the list must have a pointer to the next object, so each object consumes at least one pointer size of memory more than the vector.

2 for sequential traversal operation, the two should be very different, internal for all is a simple pointer operation. The following procedures are used to test.

/**
* List_vs_vector.cpp
* Created:2009-10-26 11:57
* Author:noock Tian (noock.tian@gmail.com)
* A Performance test program for Std::list vs Std::vector
*/
#include
#include
#include
#include
#include
#include
using namespace Std;

Const char* test_data[]={
    "Hello"
   , "Hello C + + guys"
  & nbsp , "Hello C + + guys from noock Tian"
   , "Hello C + + from Noock Tian in Zheng Zhou, Henan Province, Chi Na "
   ," This is a test string as a string object for performance testing between Std::vector and STD :: List "
};

Template
void Test_add (Long long repeat)
{
    cout<< "Start test_add (" < & nbsp;  size_t Tstart = Clock (), tend;
    {
        T v;
        for (long ll = 0; ll             for (int i=0; i                 V.push_back (String (Test_data[i]));
           }
       }
   }

    tend = clock ();
    cout<< "Result:start =" <    T v;
    for (int i=0; i        v.push_back (String (test_data[i));
   }
    size_t tstart = Clock (), tend;
    for (long-ll = 0; ll        for (TypeName T::const_ Iterator it = V.begin (); It!= v.end (); ++it) {
            strcpy (char*) Null_buff, it->c_ STR ());
       }
   }
    tend = clock ();
    cout<< "Result:start =" <    test_add > (repeat);
    test_sequantial_traval > (repeat);

cout<< "/n========= list ==========" < Test_add > (repeat);
Test_sequantial_traval > (repeat);

int C;
cout<< "Enter a letter to exit" < cin>>c;

return 0;
}

The results on the VS2010BETA2 are as follows:

=========vector:==========
Start Test_add (100000)
Result:start = 27; end = 401; Cost = 374
Start Test_travel (100000)
Result:start = 438; end = 473; Cost = ========= list ==========
Start Test_add (100000)
Result:start = 494; end = 881; Cost = 387
Start Test_travel (100000)
Result:start = 902; end = 940; Cost = 38
In Cygwin, using GCC 3.4.4 test results are as follows:
=========vector:==========
Start Test_add (100000)
Result:start = 31; end = 2916; Cost = 2885
Start Test_travel (100000)
Result:start = 2916; end = 2978; Cost = ========= list ==========
Start Test_add (100000)
Result:start = 2978; end = 5521; Cost = 2543
Start Test_travel (100000)
Result:start = 5521; end = 5584; Cost = 63
The results show that the efficiency of the two is equivalent to sequential addition and sequential traversal, and that the effectiveness of the compiler is related to the implementation of its standard library, but the list consumes more memory and may cause more memory fragmentation, so using vector is a better choice.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.