3

I don't know my exact numbers but i'll try my best. I have a 10000 element deque thats populated right at the start. Than i scan through each element and lets every 20 elements i'll need to insert an new element. The insert would happen at the current position and maybe one element back.

I don't exactly need to remember the position but i also don't exactly need random access either. I'd like fast inserts. Does deque and vector have a heavy price to pay on insert? Should i use list?

My other option is to have a 2nd deque list and as i go through each element insert it to the other deque list unless i need to do the insert i am talking about. This does need to be fast as its a performance intensive app. But I am using a lot of pointers (each element is a pointer) which is upsetting me but there isn't a way around that so i should assume L1 cache will always miss?

6 Answers 6

4

I'd start with std::vector in this case, but use a second std::vector for your mass mutations, reserve() appropriately, then swap() the vectors.

Update

It would take this general form:

std:vector<t_object*> source; // << source already holds 10000 elements

std:vector<t_object*> tmp;

// to minimize reallocations and frees to 1 and 1, if possible.
// if you do not swap or have to grow more, reserving can really work against you.
tmp.reserve(aMeaningfulReserveValue);

while (performingMassMutation) {
  // "i scan through each element and lets every 20 elements"
  for (twentyElements)
    tmp.push_back(source[readPos++]);

  // "every 20 elements i'll need to insert an new element"
  tmp.push_back(newElement);
}

// approximately 500 iterations later…

source.swap(tmp);

Borealid brought up a good point, which is measure -- execution varies dramatically depending on your std library implementations, data sizes, complexity to copy, and so on.

For raw pointers of a collection this size with my configuration, the vector mass mutation and push_back above was 7 times faster than std::list insertion. push_back was faster than vector's range insertion.

As Emile points out below, std::vector::swap() does not need to move or reallocate elements -- it can just swap out internals (provided the allocators are the same type).

6
  • hmm, swapn instead of copy. Good idea. They are all pointers of some kind so swap would either not matter or be slower (from zeroing out the other entry). But i wouldnt have thought swap if it wasnt a pointer. Good answer.
    – user34537
    Feb 12, 2012 at 8:12
  • 3
    @acidzombie24: vector::swap is constant time. Only two or three pointers are exchanged between the vectors, even if they each have millions of elements. Those pointers would be something like m_start, m_end, and m_storage_end. vector::swap would be blazingly fast even if you were storing huge objects by value in your vectors. Feb 12, 2012 at 8:44
  • If there are multiple passes of your algorithm, you can keep on using the same two vectors in a ping-pong fashion. Feb 12, 2012 at 8:46
  • Thanks for the ping. Oh i get it.
    – user34537
    Feb 12, 2012 at 8:47
  • @acidzombie24 you're welcome. i updated again after my curiosity got the better of me and I did some testing =) enjoy!
    – justin
    Feb 12, 2012 at 10:30
3

First off, the answer to all performance questions is "benchmark it". Always. Now...

If you don't care about the memory overhead, and you don't need random access, but you do care about having constant-time insertions, list is probably right for you.

std::vector will have constant-time insertions at the end when it has sufficient capacity. When the capacity is exceeded, it needs a linear-time copy. deque is better because it links discrete allocations, avoiding a complete copy and letting you do constant-time insertions at the front as well. Random insertions (every 20 elements) will always be linear time.

As for cache locality, a vector is as good as you can get (contiguous memory), but you said you cared about insertions rather than lookups; in my experience, when that's the case you don't care about how hot the cache gets as you scan through to dump, so list's poor behavior doesn't much matter.

6
  • 1
    I don't agree with you. Vector is always linear if we speak of insertion at a random position. Vector is always constant in amortized speed only when you do insertion at the end of the container. Vector will be of terrific performance in the case. Feb 12, 2012 at 8:06
  • @BorisStrandjev I clearly bounded my statement - when you call reserve to a capacity larger than the vector's size, the next insertion at the end will be constant time. That is part of the C++ standard. And "amortized constant" does not mean that a particular insertion is constant time - which is what I said.
    – Borealid
    Feb 12, 2012 at 8:07
  • Do you think a list is better than having a 2nd container (vector likely) which i build as i go through?
    – user34537
    Feb 12, 2012 at 8:10
  • @Borealid - true I missed the phrase at the end. Maybe because it was bold :P? However, I think in the case we are more worried in the amortized speed of series of operations not a single operation. Also I do not see a reason why somebody will call reserve in this case, because the default behavior of the container already gets us to the same time complexity and nobody has to think of how many elements to reserve at first place (nothing to mention how many more when the reserve runs out). Feb 12, 2012 at 8:15
  • 1
    The comment about deque is wrong. When the capacity is full, it's just another "page" that gets allocated, the previous elements don't need to be copied. A deque is basically an array of pointers to pages where a fixed number of elements resides in.
    – Xeo
    Feb 12, 2012 at 8:37
2

Lists are useful when either you frequently want to insert elements in the middle of the collection, or frequently remove them. Lists are, however, slow to read.

Vectors are very fast to read and very fast when you only want to add or remove elements at the end of the collection, but they are very slow when you insert elements in the middle. This is because it has to move all elements after the desired position by one place, to make room for the new element.

Deques are basically doubly linked lists that can be used as vectors.

If you don't need to insert elements in the middle of the collection (you don't care about the order), I suggest you use vector. If you can approximate the number of elements that will be introduced in the vector from the beginning, you should also use std::vector::reserve to allocate memory necessary from the beginning. The value you pass to reserve doesn't need to be exact, just approximate; if it's smaller than needed, the vector will resize automatically, when necessary.

2

You can go two ways: list is always an option for random place insertions, however as you allocate every element separately this will cause some performance implications too. The other option of inserting in-place in the deque is not good as well - because you will pay linear time for every insertion. Maybe your idea of inserting in new deque is the best here - you pay twice as much memory, but on the other hand you always do insertion either at the end of the second deque, or one element before that - this all gives constant amortized time, and still you have good caching of the container.

2

The number of copies done for std::vector/deque ::insert etc is proportional to the number of elements between the insert position and the end of container (the number of elements that need to be shifted to make room). The worst-case for a std::vector is O(N) - when you insert at the front of the container. If you're inserting M elements the worst -case is therefore O(M*N) which isn't great.

There could also be a reallocation involved if the containers capacity is exceeded. You could prevent reallocation by ensuring that sufficient space was ::reserve'd up front.

You're other suggestion - copying to a second std::vector/deque container could be better in that it could always be organised to achieve O(N) complexity, but at the cost of temporarily storing two containers.

Using a std::list would allow you to achieve in-place O(1) inserts, but at the cost of additional memory overhead (storing the list pointers etc) and reduced memory locality (list nodes are not allocated contiguously). You could improve the memory locality by using a pool'd memory allocator (Boost pools maybe?).

Overall you'd have to benchmark to really sort out which is "the fastest" approach.

Hope this helps.

2
  • Its funny that people are saying i'd pay the cost of two containers but I have no idea if a list<void*> -is- as big as 2 vector<void*> or not.
    – user34537
    Feb 12, 2012 at 8:18
  • 2
    @acidzombie24: Well I would expect most list implementations to contain two pointers per node + the actual data, so a rough estimate of memory use would be: 2 vectors = 2 * N * sizeof(data_type). 1 list would be: N * sizeof(data_type) + 2 * N * sizeof(void *). If the size of your data_type was small (I think you said it's just a pointer) the list could use more memory... Feb 12, 2012 at 8:26
1

If you need fast inserts in the middle, but don't care about random access, vector and deque are definitely not for you: For those, every time you insert something, all elements between that one and the end have to be moved. Of the built-in containers, list is almost certainly your best bet. However a better data structure for your scenario would probably be a VList because it provides better cache locality, however that's not provided by the C++ standard library. The Wikipedia page links to a C++ implementation, however from a quick view on the interface it doesn't seem to completely STL compatible; I don't know if this is an issue for you.

Of course, in the end the only way to be sure which is the optimal solution is to measure the performance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.