Thread: Which is better ?

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    Which is better ?

    Hi,

    Due to my lack of understanding of how vectors work i have a question regarding cleaning vectors. The situation is the following:


    Is it better to do this :
    Code:
    while(){
    vector<int>vec;
    
    //fill vector
    }
    or

    Code:
    vector<int>vec;
    
    while(){
      vector<int>().swap(vec)
    //fill vector
    }
    when i say better i mean faster since my vectors are quite large and they need to be filled and emptied many times over.

    thnx

    baxy

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by baxy
    when i say better i mean faster since my vectors are quite large and they need to be filled and emptied many times over.
    It is unlikely for the former to be worse than the latter, but if you want to be sure then measure. Also, you might to consider just reusing the same vector object since it is not like your elements have destructors that need to be called.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Like laserlight said, there is probably not much difference between your options: both involve a destruction, a construction, and a reallocation (just in different orders).

    I'd also suggest considering the use of "vec.resize(0)", with vec defined outside the loop.

    The destruct/construct/reallocate cycle is often not particularly efficient if subsequent operations then reallocate large blocks of memory of similar size. It is not specified whether resize(0) actually releases any resources or not but avoiding that cycle can't hurt.

    Note I used the word "often". The specific advantages and disdvantages depend on how the particular library implements std::vector and its allocators (so vary between implementations).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    If the vector is huge enough that freeing it makes a material difference to the memory pressure on the system as a whole, then that's a different discussion actually. You will not get that sort of deterministic memory behavior with an STL container. Or even with malloc() and free() in some cases.

    If you are just "emptying" the vector so you can use its size() for algorithmic purposes, then calling resize(0) or clear() is all you need to do.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Agreed brewbuck, the faster option is to declare the vector outside of the loop, and inside the loop, simply use .resize(0)
    This then normally avoids reallocating the vector's storage space unless the vector needs to grow.

    In fact, I made this exact optimisation today at work!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Tags for this Thread