STL Vector Efficiency

The STL vector class stores a copy of the object using the copy constructor every time push_back is called. Wouldn't that slow down the program? I may have a special linked list class that relates to pointers to objects. Although it would not have any STL advantages, it should still be faster.

See this code below:

#include <vector> #include <iostream> #include <cstring> using namespace std; class myclass { public: char* text; myclass(const char* val) { text = new char[10]; strcpy(text, val); } myclass(const myclass& v) { cout << "copy\n"; //copy data } }; int main() { vector<myclass> list; myclass m1("first"); myclass m2("second"); cout << "adding first..."; list.push_back(m1); cout << "adding second..."; list.push_back(m2); cout << "returning..."; myclass& ret1 = list.at(0); cout << ret1.text << endl; return 0; } 

its output is output as:

 adding first...copy adding second...copy copy 

The output shows that the copy constructor is called both when adding and when receiving a value even then. Does this affect esp performance when we have larger objects?

+4
source share
11 answers

Copying will affect performance. If you store large objects in standard containers, it will be useful to use smart pointers instead of the objects themselves.

+10
source

Something other posters haven't talked about is that you can use vector :: reserve to predefine the memory area for a vector if you have an idea of ​​how many elements are needed in your vector. This should speed things up, especially if you use push_back to store objects by value, because the vector class will not need to reallocate a new contiguous block of memory when it reaches the capacity limit in the loop.

+5
source

Yes Yes. This is one of the main reasons why we will get rvalue links in C ++ 0x.

+4
source

If you do not store the base types, it is recommended that you use pointers as your vector elements instead of the actual objects themselves. And in many cases it is better to use smart pointers. Copy ctor will still be called - not from your class, but from the smart pointer class.

+3
source

It all depends on how big your object is. But in most cases, I would say that it is better to use the semantics of values ​​(copy all objects), and it should be quite high. This eliminates the need to think about memory management associated with pointers.

ONLY if you find that performance is insufficient, you should consider using pointers, and then carefully study memory management.

And if you need pointers, just use vector<myclass*> rather than deploying your own collection class. What is the beauty of STL generic =)

+1
source

In a C ++ 0x note, std :: vector and friends will get emplace_back (), which will put the element in place at the end of the vector.

http://msdn.microsoft.com/en-us/library/dd647620.aspx

+1
source

You may not believe it, but vector (and better still deque ) are the fastest containers that STL can offer for most tasks.

You may worry about copying your object, but if it is not extremely huge or the Copy Constructor is a little complicated, it will cost less to copy the object that allocates it to the heap.

Heap allocation and omissions in the result cache are much larger than a simple copy.

But let's not chat in standby mode: compare your container with vector and see what is in the top, and at what level of magnitude, I'm sure you will be surprised.

And if your class is really huge or forbidden to copy, there is always boost::ptr_vector , although if you do not use the pool, you obviously throw the cache in the window;)

+1
source

Copying will have some impact on performance. For small objects, just save them by value, and the profiling will tell you if you need to reconsider. For larger objects, you can save a smart pointer in a container to reduce some problems. Again, if preserving an object by value is natural, do it so that profiling does not mean it is a significant bottleneck.

Also note that I do not believe the return does make a copy. What you see is that β€œadding a second” probably makes two copies: first, it should expand the vector from 1 to (possibly) 2 and copy the old element from its previous location to the new one, and then copy item 2 to its place.

0
source

Copying objects can have unforeseen side effects. Reality are C ++ classes that are often not written for copying, and therefore they can cause errors ... the idealist will argue that "they must write their classes correctly", but I prefer to deal with it in fact, it’s not even will be so rare. Not to mention the fact that any kind of log that your class does to create / destroy will happen a lot.

In essence, this is not what the encoder had to do, that the copy was made. This in itself means that this is a bad plan for non-trivial types. For simple cases, this is normal (say, a Point2D class), but be careful and generally, if the class does not carry a small amount of data, store pointers (or use smart pointers).

0
source

You should choose the container with the best performance characteristics for your use case. Your use case looks like you need to add elements to the end of the container. With the help of vector you sometimes get redistribution and performance if you don’t know in advance the maximum number of participants and you can afford reserve in advance.

If you use deque instead, you are guaranteed to constantly set the time on the back (or front) capacity, while maintaining useful features such as random access.

If you want something semantically, the container of objects changing to a pointer vector purely for performance reasons will not be my recommendation, indeed, this may not be a performance gain, since each object requires a separate memory allocation, as opposed to using an object container, where memory for several objects can be allocated in advance.

0
source

First, it is recommended that you use pointers instead, using your own class in the vector. However, here, since the text data is stored as a pointer. Its value (the text value of the attribute) is not copied twice. Therefore, there are no serious performance issues in this application.

-1
source

Source: https://habr.com/ru/post/1313046/


All Articles