Archived

This topic is now archived and is closed to further replies.

Xeee

Which is faster vector<T *> or vector<T> ?

Recommended Posts

I was Wondering Which is more efficient, because, while the first - vector for example - avoids a copy whose time is directly proportional to sizeof(Model), it decreases the spatial locality of reference, just like LinkedLists, and so ... the cache is much useless, so insertion is cheap, very cheap, but retrieval will probably cause a cache miss. On the other side, the second term - vector - involves a copy of sizeof(Model) each time you insert anything, but it emphasises on the concept of spatial locality of reference, and the data is contiguous in memory, improving cache performance. so insertion is expensiveof(Model) increases), and retrieval is good, it won''t cause a sh one to use and when? Note : feel free to correct me if i''m wrong coz i''m not good at hardware and architecture xee..

Share this post


Link to post
Share on other sites
quote:
Original post by Jedyte
A container of pointers will use up less memory, yes.
What are you smoking? Each entry will require an extra 4 bytes of memory, for the pointer itself. And that isn''t even mentioning the extra padding that can result from noncontiguous dynamic allocation.

quote:
I don''t know about cache fragmentation, but I doubt you really have to worry about it.
Ah, such a rigorous line of logic. Anyway, right here cache fragmentation can become a problem, especially if you''re iterating through the list in an inner loop and the elements were allocated over a long period of time.

The inefficiency of vector insertion is not an intractable problem, even without using pointers. If you construct the object in place, the compiler will often use return-value optimization along with copy-constructor optimization to gain you some precious microseconds. Like so:

myVector.push_back(Model(3, 5.0, false, "Yummy cookie"));

In this case, a good compiler will shove the constructor inline into the inlined push_back().


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites