locked
Performance clarification on vectors? RRS feed

  • Question

  • Hi,

    The current application i'm working on has a single vector having a sequence of elements with data as well as a flag set (true/false). If flag is true , i'll add the vector data to string A else string B.

    Is there a performance difference of having 2 vectors rather than having a single vector or it doesn't matter?

    Thanks in advance,

    Regards,
    Mayur M

    Friday, February 27, 2009 4:56 PM

Answers

  • Hello Mayur,

    I would like to let you know a couple of things.

    1. First of all, vector is not an insertion friendly container. The reason is, if an item is to be inserted and if vector doesn't have reserved space, it will reallocate the entire contents which will cause performance overhead.
    2. If you are so keen about performance, then use std::list. But keep in mind that, list won't support random accessing of elements like vector does. In other words, it doesn't support [] operator.
    3. if you still needs to use vector, then i'll recommend to go for two vectors, since it will reduce the reallocation effort[ only if you are so worried about performance].
    4. One addon tip to reduce reallocation is to reserve a good amount of memory by calling - vector::reserve()
    5. Since you have vector of pointer, i think you don't have much performance overhead. But tuning to maximum is always good.
    Hope it helps you.

    Regards,
    Jijo.
    _____________________________________________________

    http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
    Friday, February 27, 2009 5:56 PM
  • It's really hard to comment on performance issues when there is so little known about the application in general. This may very well be a case of "premature optimization", as suggested by Tony Hoare (and restated by Donald Knuth). See the section of "When to optimize" found here.

    I think you should concentrate on design first. A vector of pointers is generally discouraged because cleanup is messy. Make it a vector of classes instead, and write a good copy constructor and assignment operator. For most small classes the performance is adequate.

     

     

    Friday, February 27, 2009 6:33 PM

All replies

  • I think you need to show a little code snippet to convey your question better. I can't make sense of this post.

     

    Friday, February 27, 2009 5:18 PM
  • vector<SampleClass*> chunks;  
     
    if (true)  
    {  
        SampleClass* chunk = new SampleClass( s, true );  
        chunks.push_back( chunk );  
    }  
    else if (false)  
    {  
        SampleClass* chunk = new SampleClass( s, false );  
        chunks.push_back( chunk );  

    If you can see the above code  for both the the cases i'm adding to the same vector.

    Friday, February 27, 2009 5:33 PM
  • Hello Mayur,

    I would like to let you know a couple of things.

    1. First of all, vector is not an insertion friendly container. The reason is, if an item is to be inserted and if vector doesn't have reserved space, it will reallocate the entire contents which will cause performance overhead.
    2. If you are so keen about performance, then use std::list. But keep in mind that, list won't support random accessing of elements like vector does. In other words, it doesn't support [] operator.
    3. if you still needs to use vector, then i'll recommend to go for two vectors, since it will reduce the reallocation effort[ only if you are so worried about performance].
    4. One addon tip to reduce reallocation is to reserve a good amount of memory by calling - vector::reserve()
    5. Since you have vector of pointer, i think you don't have much performance overhead. But tuning to maximum is always good.
    Hope it helps you.

    Regards,
    Jijo.
    _____________________________________________________

    http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
    Friday, February 27, 2009 5:56 PM
  • It's really hard to comment on performance issues when there is so little known about the application in general. This may very well be a case of "premature optimization", as suggested by Tony Hoare (and restated by Donald Knuth). See the section of "When to optimize" found here.

    I think you should concentrate on design first. A vector of pointers is generally discouraged because cleanup is messy. Make it a vector of classes instead, and write a good copy constructor and assignment operator. For most small classes the performance is adequate.

     

     

    Friday, February 27, 2009 6:33 PM