clearing a vector or defining a new vector, which one is faster

后端 未结 2 1758
一向
一向 2021-02-05 10:26

Which method is faster and has less overhead?

Method 1:

void foo() {
  std::vector< int > aVector;
  for ( int i = 0; i < 1000000; ++i ) {
              


        
相关标签:
2条回答
  • 2021-02-05 10:52

    To create an empty vector is very little overhead. To GROW a vector to a large size is potentially quite expensive, as it doubles in size each time - so a 1M entry vector would have 15-20 "copies" made of the current content.

    For trivial basic types, such as int, the overhead of creating an object and destroying the object is "nothing", but for any more complex object, you will have to take into account the construction and destruction of the object, which is often substantially more than the "put the object in the vector" and "remove it from the vector". In other words, the constructor and destructor for each object is what matters.

    For EVERY "which is faster of X or Y" you really need to benchmark for the circumstances that you want to understand, unless it's VERY obvious that one is clearly faster than the other (such as "linear search or binary search of X elements", where linear search is proportional to X, and binary search is log2(x)).

    Further, I'm slightly confused about your example - storing ONE element in a vector is quite cumbersome, and a fair bit of overhead over int x = i; - I presume you don't really mean that as a benchmark. In other words, your particular comparison is not very fair, because clearly constructing 1M vectors is more work than constructing ONE vector and filling it and clearing it 1M times. However, if you made your test something like this:

    void foo() {
      for ( int i = 0; i < 1000; ++i ) {
         std::vector< int > aVector;
         for(j = 0; j < 1000; j++)
         {
             aVector.push_back( i );
         }
      }
    }
    

    [and the corresponding change to the other code], I expect the results would be fairly similar.

    0 讨论(0)
  • 2021-02-05 10:56

    The clear() is most likely to be faster, as you will retain the memory that has been allocated for previous push_back()s into the vector, thus decreasing the need for allocation.

    Also you do away with 1 constructor call and 1 destructor call per loop.

    This is all ignoring what you're compiler optimizer might do with this code.

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