I'm adding about 25,000 objects to a vector and it's taking too long so I wanted to try and speed it up. Currently I'm adding them in the format:

vector<MyClass> MyVector;
MyVector.push_back(MyClass(Param1, Param2, Parm3, Param4));

As I understand this would involve creating two instances of the object and copying one to the other. Is that correct?

Would it run quicker if I change the vector to pointers like this:

vector<MyClass*> MyVector;
MyVector.push_back(new MyClass(Param1, Param2, Parm3, Param4));

Thanks for any help you can offer.

vector<MyClass> MyVector;
MyVector.push_back(MyClass(Param1, Param2, Parm3, Param4));

As I understand this would involve creating two instances of the object and copying one to the other. Is that correct?

As I see it, you are creating one instance of 'myclass' which pushed into the vector.

Would it run quicker if I change the vector to pointers like this:

vector<MyClass*> MyVector;
MyVector.push_back(new MyClass(Param1, Param2, Parm3, Param4));

One downfall I see to this method, based on the sheer number of calls to allocate new memory, you should at least be prepared to handle a bad_alloc exception. This method also involves some time for the OS to find an allocate memory for each 'new' operation.

If you want to boost your performance, try taking control of the vector memory allocation using member functions such as: max_size(), capacity(), reserve(), and resize().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.